Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.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# 可以避免在多重绑定中使用转换器吗_C#_Wpf_Xaml - Fatal编程技术网

C# 可以避免在多重绑定中使用转换器吗

C# 可以避免在多重绑定中使用转换器吗,c#,wpf,xaml,C#,Wpf,Xaml,我遇到了一种情况,希望避免在多重绑定中使用转换器,下面是我当前代码中的xaml源代码片段下面的代码可以很好地工作,但是否可以避免将转换器放在首位?? 视图模型: public MainViewModel() { Cars = new List<string>() { "Audi", "BMW", "Ferrari", "Ford" }; Models = new List<string>() { "Model 1", "Model 2" }; Is

我遇到了一种情况,希望避免在多重绑定中使用转换器,下面是我当前代码中的xaml源代码片段下面的代码可以很好地工作,但是否可以避免将转换器放在首位??

视图模型:

public MainViewModel()
{
    Cars = new List<string>() { "Audi", "BMW", "Ferrari", "Ford" };
    Models = new List<string>() { "Model 1", "Model 2" };
    IsOptionEnable = false;
}

public bool IsOptionEnable { get; private set; }
public List<string> Models { get; private set; }

public List<string> Cars { get; private set; }
<Grid>
    <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="87.2,44.8,0,0" 
              ItemsSource="{Binding Cars}" 
              SelectedItem="{Binding SelectedItm}"
              Style="{StaticResource ModelsComboBox}">
    </ComboBox>
</Grid>
<Style x:Key="ModelsComboBox" TargetType="ComboBox">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ComboBoxItem">
                <Setter Property="IsEnabled">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource ModelToBoolConverter}">
                            <Binding/>
                            <Binding Path="DataContext.IsOptionEnable" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
internal sealed class ModelToBoolConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool enable = true;
        if ((values[0] != null && values[0] != DependencyProperty.UnsetValue) &&
            (values[1] != null && values[1] != DependencyProperty.UnsetValue))
        {
            var comboboxItemText = values[0] as string;
            if ((comboboxItemText == "Ferrari") && (bool)values[1] == false)
            {
                enable = false;
            }
        }

        return enable;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
  <Style x:Key="ModelsComboBox" TargetType="ComboBox">
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ComboBoxItem">
                        <Setter Property="IsEnabled" Value="True"/>
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding}" Value="Ferrari"/>
                                    <Condition Binding="{Binding Path=DataContext.IsOptionEnable,
                                               RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}"
                                               Value="False"/>
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled" Value="False"/>
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>

在这种情况下,可以使用
MultiDataTrigger

           <Style TargetType="ComboBox">
                <Setter Property="IsEnabled" Value="True" />
                <Style.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding}" Value="Ferrai"/>
                            <Condition Binding="{Binding Path=DataContext.IsOptionEnable, RelativeSource={RelativeSource AncestorType=ComboBox}}" Value="False" />
                        </MultiDataTrigger.Conditions>
                        <MultiDataTrigger.Setters>
                            <Setter Property="IsEnabled" Value="False" />
                        </MultiDataTrigger.Setters>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>

您可以使用多数据触发器来实现相同的功能

资源字典:

public MainViewModel()
{
    Cars = new List<string>() { "Audi", "BMW", "Ferrari", "Ford" };
    Models = new List<string>() { "Model 1", "Model 2" };
    IsOptionEnable = false;
}

public bool IsOptionEnable { get; private set; }
public List<string> Models { get; private set; }

public List<string> Cars { get; private set; }
<Grid>
    <ComboBox HorizontalAlignment="Left" VerticalAlignment="Top" Width="120" Margin="87.2,44.8,0,0" 
              ItemsSource="{Binding Cars}" 
              SelectedItem="{Binding SelectedItm}"
              Style="{StaticResource ModelsComboBox}">
    </ComboBox>
</Grid>
<Style x:Key="ModelsComboBox" TargetType="ComboBox">
    <Setter Property="ItemContainerStyle">
        <Setter.Value>
            <Style TargetType="ComboBoxItem">
                <Setter Property="IsEnabled">
                    <Setter.Value>
                        <MultiBinding Converter="{StaticResource ModelToBoolConverter}">
                            <Binding/>
                            <Binding Path="DataContext.IsOptionEnable" RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}"/>
                        </MultiBinding>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>
internal sealed class ModelToBoolConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool enable = true;
        if ((values[0] != null && values[0] != DependencyProperty.UnsetValue) &&
            (values[1] != null && values[1] != DependencyProperty.UnsetValue))
        {
            var comboboxItemText = values[0] as string;
            if ((comboboxItemText == "Ferrari") && (bool)values[1] == false)
            {
                enable = false;
            }
        }

        return enable;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
  <Style x:Key="ModelsComboBox" TargetType="ComboBox">
            <Setter Property="ItemContainerStyle">
                <Setter.Value>
                    <Style TargetType="ComboBoxItem">
                        <Setter Property="IsEnabled" Value="True"/>
                        <Style.Triggers>
                            <MultiDataTrigger>
                                <MultiDataTrigger.Conditions>
                                    <Condition Binding="{Binding}" Value="Ferrari"/>
                                    <Condition Binding="{Binding Path=DataContext.IsOptionEnable,
                                               RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ComboBox}}}"
                                               Value="False"/>
                                </MultiDataTrigger.Conditions>
                                <Setter Property="IsEnabled" Value="False"/>
                            </MultiDataTrigger>
                        </Style.Triggers>
                    </Style>
                </Setter.Value>
            </Setter>
        </Style>


它与已发布的答案有何不同?(距离你的答案还有6分钟)@RohitVats这只是偶尔发生的,因为SO没有通知刚刚发布的新答案。这件事现在发生在我身上。@Clemens-同意,但在发布后,您可以看到另一个答案,并立即将其删除。也发生在我身上(你可以看到我删除的答案)。如果这个答案会给发布的答案增加任何值,那么我认为发布改进的答案没有问题。@RohitVats:你是对的,这个答案与这里发布的另一个答案没有太大区别,希望在另一个答案中定义的资源是匿名资源(此样式将自动应用于所有Combobox控件)这一个你需要明确地应用,这对于这个答案来说毫无意义。OP感兴趣的是内部内容。