wpf在Groupbox IsEnabled属性中指定多个DataBinding

时间:2017-06-27 12:53:22

标签: wpf data-binding

我有一个GroupBox,其IsEnabled属性是通过ViewModel上的属性设置的,如下所示: -

<GroupBox>
    <Canvas IsEnabled="{Binding CurrentRec.Current_Selected_Category.NoBonus,Converter={StaticResource TFC}}">
        <Label Content="Amount:" Width="55" Canvas.Left="9" Canvas.Top="-2"/>
        <TextBox x:Name="txtBonusAmount" Width="76"  Canvas.Left="12" Canvas.Top="20" Text="Some text"/>
        <Label Content="Bonus:" Canvas.Top="38" Width="54" Canvas.Left="10"/>
        <TextBox x:Name="txtBonus" Width="76"  Canvas.Left="13" Canvas.Top="58" Text="Some Text"/>
    </Canvas>
<Groupbox>

我的viewmodel中有多个属性影响Canvas的IsEnabled属性。我如何根据Canvas的IsEnabled属性指定这些附加属性?

2 个答案:

答案 0 :(得分:2)

MultiBinding与转换器一起使用:

<GroupBox>
    <GroupBox.Resources>
        <local:MultiConverter x:Key="conv" />
    </GroupBox.Resources>
    <Canvas>
        <Canvas.IsEnabled>
            <MultiBinding Converter="{StaticResource conv}">
                <Binding Path="CurrentRec.Current_Selected_Category.NoBonus" />
                <Binding Path="TheOtherProperty" />
            </MultiBinding>
        </Canvas.IsEnabled>
        <Label Content="Amount:" Width="55" Canvas.Left="9" Canvas.Top="-2"/>
        <TextBox x:Name="txtBonusAmount" Width="76"  Canvas.Left="12" Canvas.Top="20" Text="Some text"/>
        <Label Content="Bonus:" Canvas.Top="38" Width="54" Canvas.Left="10"/>
        <TextBox x:Name="txtBonus" Width="76"  Canvas.Left="13" Canvas.Top="58" Text="Some Text"/>
    </Canvas>
</GroupBox>

转换器类应该实现IMultiValueConverter接口并从bool方法返回Convert

public class MultiConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        bool noBonus = System.Convert.ToBoolean(values[0]);
        bool theOtherSourceProperty = System.Convert.ToBoolean(values[1]);

        //..

        return noBonus && theOtherSourceProperty;
    }

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

答案 1 :(得分:1)

<Window.Resources>
    <local:OrConverter x:Key="OrConverter" />
    <local:AndConverter x:Key="AndConverter" />
</Window.Resources>

...

<Canvas>
    <Canvas.IsEnabled>
        <MultiBinding Converter="{StaticResource AndConverter}">
            <Binding 
                Path="CurrentRec.Current_Selected_Category.NoBonus"
                Converter="{StaticResource TFC}"
                />
            <Binding 
                Path="SomeOtherProperty"
                />
        </MultiBinding>

转换器:

public class OrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.Select(v => System.Convert.ToBoolean(v)).Any(b => b);
    }

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

public class AndConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        return values.Select(v => System.Convert.ToBoolean(v)).All(b => b);
    }

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