WPF - 绑定到一个菜单图标 [英] WPF - Binding to a Menu Icon

查看:435
本文介绍了WPF - 绑定到一个菜单图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含菜单用户控件。我需要的Menu.Icon绑定到用户控件的属性,但它不工作。

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.

在code开头是这样 -

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>

您看到声明(testImage)的第一个图像完美地工作,所以我很高兴,绑定是正确的。第二个图像(注释出来,并命名为workswhenin)包含包URI说我传递给用户控件绑定属性并且工作太多,但第三个(realImage)不会出现在所有!

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!

我看不出有任何理由不应该工作;我知道绑定是很好,我知道,在标记图像的位置是好,所以这是怎么回事?

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?

任何帮助将大大AP preciated。 先谢谢了。

Any help will be greatly appreciated. Thanks in advance.

推荐答案

不能确切地回答,因为我看不到你背后code,但我pretty的肯定,我知道是什么问题是。

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 期望类型的对象的ImageSource 。当您在XAML指定URL默认WPF转换器用于将URL转换为的ImageSource 对象。由于您使用绑定,不使用默认的转换器。所以,你可能想设置图像源的URL值,而不是一个的ImageSource 对象。

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.

在身后财产的code,你必须创建一个的ImageSource 对象,这实在是一种痛苦。您可以创建一个的BitmapImage ,并通过在URL中。

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.

最简单的解决方法是使用Microsoft的默认转换器背后财产code,你是绑定,或者用它在明确的约束。该转换器被称为 ImageSourceConverter

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.

编辑:

下面是一个简单的例子:

Here is a simple example:

绑定源里面的code:

Code inside of the binding source:

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

更新绑定瞄准这一属性,而不是在AddImage财产。请确保你火PropertyChanged事件此属性也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.

没走建立一个测试场景为这个时间,但它应该没有任何问题。

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

这篇关于WPF - 绑定到一个菜单图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆