WPF - 绑定到菜单图标

时间:2021-10-27 17:01:15

I've got a UserControl that contains a menu. I need to bind the Menu.Icon to a property of the UserControl but it's not working.

我有一个包含菜单的UserControl。我需要将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. 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!

您看到的第一个图像声明(testImage)完美无缺,因此我很高兴绑定是正确的。第二个Image(注释掉并命名为'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.

任何帮助将不胜感激。提前致谢。

1 个解决方案

#1


5  

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. 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.

Image.Source需要一个ImageSource类型的对象。在XAML中指定URL时,将使用默认WPF转换器将URL转换为ImageSource对象。由于您使用的是绑定,因此不使用默认转换器。因此,您可能尝试将图像源设置为URL值而不是ImageSource对象。

In your code behind property, you will have to create an ImageSource object, which is really a pain. You could create a BitmapImage and pass in the URL.

在你的代码隐藏属性中,你将不得不创建一个ImageSource对象,这真的很痛苦。您可以创建一个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. The converter is called ImageSourceConverter.

最简单的解决方案是在要绑定的代码隐藏属性中使用Microsoft的默认转换器,或者在绑定中明确使用它。转换器称为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. Make sure you fire the PropertyChanged event for this property also when the AddImage property changes.

更新绑定以定位此属性而不是AddImage属性。确保在AddImage属性更改时也为此属性触发PropertyChanged事件。

Didn't take the time to build a test scenario for this, but it should work without any problems.

没有花时间为此构建测试场景,但它应该没有任何问题。

#1


5  

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. 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.

Image.Source需要一个ImageSource类型的对象。在XAML中指定URL时,将使用默认WPF转换器将URL转换为ImageSource对象。由于您使用的是绑定,因此不使用默认转换器。因此,您可能尝试将图像源设置为URL值而不是ImageSource对象。

In your code behind property, you will have to create an ImageSource object, which is really a pain. You could create a BitmapImage and pass in the URL.

在你的代码隐藏属性中,你将不得不创建一个ImageSource对象,这真的很痛苦。您可以创建一个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. The converter is called ImageSourceConverter.

最简单的解决方案是在要绑定的代码隐藏属性中使用Microsoft的默认转换器,或者在绑定中明确使用它。转换器称为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. Make sure you fire the PropertyChanged event for this property also when the AddImage property changes.

更新绑定以定位此属性而不是AddImage属性。确保在AddImage属性更改时也为此属性触发PropertyChanged事件。

Didn't take the time to build a test scenario for this, but it should work without any problems.

没有花时间为此构建测试场景,但它应该没有任何问题。