Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于其他属性禁用WPF组合框项_C#_Wpf - Fatal编程技术网

C# 基于其他属性禁用WPF组合框项

C# 基于其他属性禁用WPF组合框项,c#,wpf,C#,Wpf,在从枚举填充的WPF应用程序中有一个组合框。 如何禁用某些项目?禁用必须是动态的,基于ViewModel中的另一个属性 <ComboBox ItemsSource="{utility:EnumMarkupExtension {x:Type types:SomeEnum}}" SelectedItem="{Binding Path=MySelection, Converter={StaticResource SomeEnumToString}}" /> (Some

在从枚举填充的WPF应用程序中有一个组合框。 如何禁用某些项目?禁用必须是动态的,基于ViewModel中的另一个属性

<ComboBox ItemsSource="{utility:EnumMarkupExtension {x:Type types:SomeEnum}}"
          SelectedItem="{Binding Path=MySelection, Converter={StaticResource SomeEnumToString}}" />
SomeEnumToString
是一个可能与此无关的
IValueConverter

有没有什么我不知道的方法

我见过这样的解决方案
但是无法确定如何将属性传递给
IValueConverter
,因为
ConverterParameter
是不可绑定的。

您可以使用
多绑定
,其中一个值不是
ConverterParameter
。请参见此

编辑ComboboxItemTemplate,在item template中,使用viewmodel(它应该是索引)的值绑定IsEnabled,并使用转换器参数计算项目本身的值。

我知道您已在@clemens的帮助下解决了此问题。仍在为可能会回答相同问题的其他人发布答案

假设VMDataSource是Viewmodel中的数据源,并且VMDataSource中的每个项都包含一个布尔标志,以指示是否启用/禁用该项。下面的代码片段可以在您在文章中查询时使用

    <ComboBox ItemsSource="{Binding VMDataSource}">
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="IsEnabled" Value="{Binding Path="VMDisableItem"/>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox> 

禁用项意味着将单个ComboBoxItem的IsEnabled属性绑定到视图模型项的属性。这将在ItemContainerStyle中完成。它需要将ItemsSource绑定到一个视图模型项的集合,该集合具有该绑定的源属性。如果您根据viewmodel中您希望禁用combobox中项目的属性类型进行共享,您将能够提出一些建议。@Clemens谢谢,我明天会试试这个。我想我必须重写填充组合的部分。用于禁用某些项的属性只是一个布尔值。它应该是一个带有更改通知的布尔属性,因为它“必须是动态的”。我已经忘记了这一点。你说得对,我用的是Prism,我会在上面做一个
SetProperty
。谢谢,我想我看到了,我也会试试。谢谢,我不确定我是否理解,这和对这个问题的评论是一样的吗?谢谢!我使用
DataTrigger
做了一些稍微不同的事情,但这似乎没有必要,您的方法更简洁。我还对项目类型使用了
INotifyPropertyChanged
。您发现它很有用。
    <ComboBox ItemsSource="{Binding VMDataSource}">
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="IsEnabled" Value="{Binding Path="VMDisableItem"/>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox> 
    <ComboBox DataContext="{StaticResource viewmodelInstance}"
        ItemsSource="{Binding VMDataSource}">
        <ComboBox.Resources>
            <local:Disabler x:Key="disabler"/>
        </ComboBox.Resources>
        <ComboBox.ItemContainerStyle>
            <Style TargetType="ComboBoxItem">
                <Setter Property="IsEnabled">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource disabler}">
                            <!--Here you can bind the proper u want to use in -->
                            <!--converter to enable/ disable comboboxitem-->
                            <!--Currently it is bound property of viewmodelInstance that as non-boolean property-->                               
                            <Binding 
                                RelativeSource="{RelativeSource Mode=FindAncestor,AncestorType=ComboBox}"
                                Path="DataContext.VMDisableItem"/>


                            <!--Bind to the current comboboxitem which needs to enabled/disabled-->
                            <Binding />
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </ComboBox.ItemContainerStyle>
    </ComboBox>
class Disabler : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return !Equals(values[0], values[1]);
    }

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