简体   繁体   English

仅当图像的 Source 属性不是 null 时,如何启用菜单项?

[英]How do I enable menu items only when an Image's Source property is not null?

What is the best way to only enable menu items when an image's Source property is not null?当图像的Source属性不是 null 时,仅启用菜单项的最佳方法是什么? Here is the XAML:这是 XAML:

    <Window.Resources>
        <local:IsNullValueConverter x:Key="IsNullValueConverter" />
        <local:NotNullValueConverter x:Key="NotNullValueConverter" />
    </Window.Resources>
    <DockPanel>
        <StackPanel DockPanel.Dock="Top" Orientation="Horizontal">
            <Button Content="Fill" IsEnabled="{Binding ElementName=TheImage, Path=Source, Converter={StaticResource IsNullValueConverter}}" Click="Fill_Click" />
            <Button Content="Erase" IsEnabled="{Binding ElementName=TheImage, Path=Source, Converter={StaticResource NotNullValueConverter}}" Click="Erase_Click" />
        </StackPanel>
        <Grid DockPanel.Dock="Bottom" Background="White">
            <Grid.ContextMenu>
                <ContextMenu>
                    <MenuItem Header="Fill" IsEnabled="{Binding ElementName=TheImage, Path=Source, Converter={StaticResource IsNullValueConverter}}" Click="Fill_Click" />
                    <MenuItem Header="Erase" IsEnabled="{Binding ElementName=TheImage, Path=Source, Converter={StaticResource NotNullValueConverter}}" Click="Erase_Click" />
                </ContextMenu>
            </Grid.ContextMenu>
            <Image Name="TheImage" />
        </Grid>
    </DockPanel>

and here is the code:这是代码:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Fill_Click(object sender, RoutedEventArgs e)
        {
            int width = 1;
            int height = 1;
            var format = PixelFormats.Gray8;
            var pixels = new byte[width * height * (format.BitsPerPixel / 8)];
            TheImage.Source = BitmapSource.Create(width, height, 96.0, 96.0, format, null, pixels, width * (format.BitsPerPixel / 8));
        }

        private void Erase_Click(object sender, RoutedEventArgs e)
        {
            TheImage.Source = null;
        }
    }

    public class IsNullValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

    public class NotNullValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}

The buttons react exactly how I want: only fill is enabled initially, then once you click it and the image is filled, it is disabled while erase becomes enabled.这些按钮的反应完全符合我的要求:最初仅启用填充,然后单击它并填充图像后,它被禁用,而擦除启用。 However the context menu items are both enabled the entire time, despite their IsEnabled properties using the exact same binding as the buttons.然而,上下文菜单项都始终处于启用状态,尽管它们的IsEnabled属性使用与按钮完全相同的绑定。

see this post , use the Tag and placement target to help with the binding limitation看到这篇文章,使用标签和放置目标来帮助解决绑定限制

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

相关问题 如何仅在列表中的项目的子范围上分配属性? - How do I assign a property on only a subrange of items in a list? 如何将图像的源绑定到附加属性? - How to bind an Image's Source to an attached property? 如何将静态项添加到绑定到现有数据源的上下文菜单中? - How do I add static items to a context menu that binds to an existing data source? 如何最多一次启用菜单项? - How can I enable the menu items at most once? 反序列化XML时如何不允许属性为null或为空? - How do I not allow a property to be null or empty when deserializing XML? 如何将wpf的Image Source属性设置为canvas而不是image url? - How can i set wpf's Image Source property to canvas instead of image url? 仅当 listView 不为空时,我如何才能激活/启用上下文菜单条菜单? - How can i activate/enable contextmenustrip menu only if the listView is not empty? C#:如何将Memory stream设置为asp.net图像控件的source属性? - C# :How can i set Memory stream as the asp.net image control's source property? 当我的源 class 具有名为“原始”的属性并且我的目标是记录时,为什么我会收到 null 引用异常? - Why do I get a null reference exception when my source class has a property called "Original" and my destination is a record? 选择列表中的项目时如何将属性值设置为空 - How to set a property value to null when select items in a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM