简体   繁体   English

WPF - 绑定到菜单图标

[英]WPF - Binding to a Menu Icon

I've got a UserControl that contains a menu.我有一个包含菜单的 UserControl。 I need to bind the Menu.Icon to a property of the UserControl but it's not working.我需要将 Menu.Icon 绑定到 UserControl 的属性,但它不起作用。

The code starts like this -代码是这样开始的 -

        <Border Grid.Row="0">            
        <DockPanel>
            <Image x:Name="testImage" Height="16" Width="16" Source="{Binding ElementName=UC,Path=AddImage}"/>
            <Menu DockPanel.Dock="Left" Height="20"
              VerticalAlignment="Center">
                <MenuItem Header="{Binding ElementName=UC,Path=AddText}">
                    <MenuItem.Icon>
                        <!--<Image x:Name="workswhenin" Height="16" Width="16" Source="pack://application:,,/Kowdox;component/Images/UserIcons/user_add.png"/>-->

                        <Image x:Name="realImage" Height="16" Width="16"
                        Source="{Binding ElementName=UC,Path=AddImage}"/>
                    </MenuItem.Icon>
                </MenuItem>

The first Image you see declared (testImage) works perfectly so I'm happy that the binding is correct.您看到声明的第一张图片 (testImage) 完美运行,所以我很高兴绑定是正确的。 The second Image (commented out and named 'workswhenin') contains the pack URI that I'm passing to the UserControls bound property and that works too but the third one (realImage) doesn't appear at all!第二个图像(注释掉并命名为“workswhenin”)包含我传递给 UserControls 绑定属性的包 URI,它也可以工作,但第三个(realImage)根本没有出现!

I can't see ANY reason why it shouldn't work;我看不出它不应该起作用的任何原因; i know the binding is good and i know that the image's placement in the markup is good so what's going on?我知道绑定很好,并且我知道图像在标记中的位置很好,所以发生了什么?

Any help will be greatly appreciated.任何帮助将不胜感激。 Thanks in advance.提前致谢。

Can't tell for sure because I can't see your code behind, but I'm pretty sure I know what the problem is.无法确定,因为我看不到您的代码,但我很确定我知道问题出在哪里。

Image.Source expects an object of type ImageSource . Image.Source需要一个ImageSource类型的 object 。 When you specify the URL in XAML a default WPF converter is used to convert the URL into an ImageSource object. When you specify the URL in XAML a default WPF converter is used to convert the URL into an ImageSource object. Because you are using a binding, the default converter is not used.因为您使用的是绑定,所以不使用默认转换器。 So you are probably trying to set the image source to a URL value instead of an ImageSource object.因此,您可能正在尝试将图像源设置为 URL 值,而不是ImageSource object。

In your code behind property, you will have to create an ImageSource object, which is really a pain.在您的属性代码中,您将不得不创建一个ImageSource object,这真的很痛苦。 You could create a BitmapImage and pass in the URL.您可以创建一个BitmapImage并传入 URL。

The easiest solution is to use Microsoft's default converter in the code behind property that you are binding to, or use it in the binding explicitly.最简单的解决方案是在要绑定的属性的代码隐藏中使用 Microsoft 的默认转换器,或者在绑定中显式使用它。 The converter is called ImageSourceConverter .该转换器称为ImageSourceConverter

EDIT:编辑:

Here is a simple example:这是一个简单的例子:

Code inside of the binding source:绑定源内的代码:

public ImageSource AddImageSource
{
    get
    {
        ImageSourceConverter imgConv = new ImageSourceConverter();
        return imgConv.ConvertFrom(this.AddImage);
    }
}

Update the bindings to target this property instead of the AddImage property.更新绑定以针对此属性而不是 AddImage 属性。 Make sure you fire the PropertyChanged event for this property also when the AddImage property changes.确保在 AddImage 属性更改时也为此属性触发 PropertyChanged 事件。

Didn't take the time to build a test scenario for this, but it should work without any problems.没有花时间为此构建测试场景,但它应该可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM