【问题标题】:wpf specify multiple DataBindings in Groupbox IsEnabled Propertywpf 在 Groupbox IsEnabled 属性中指定多个 DataBindings
【发布时间】:2017-11-30 12:56:35
【问题描述】:

我有一个 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>

我的视图模型中有多个属性会影响 Canvas 的 IsEnabled 属性。如何针对 Canvas 的 IsEnabled 属性指定这些附加属性?

【问题讨论】:

  • 将 MultiBinding 与转换器一起使用。
  • 使用多值转换器,如this answer 中的示例。

标签: wpf data-binding


【解决方案1】:

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 接口并从Convert 方法返回bool

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();
    }
}

【讨论】:

    【解决方案2】:
    <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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-17
      • 1970-01-01
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2011-02-07
      • 1970-01-01
      相关资源
      最近更新 更多