根据wpf中其他控制器的属性值更改属性控制器

问题描述 投票:0回答:1

考虑到我的wpf应用程序,我有一个复选框和2个textedit,如下所示:

<CheckBox x:Uid="Checkbox_1" FlowDirection="RightToLeft" IsChecked="{Binding TickCheckBox, Mode=TwoWay}" Style="{StaticResource StandardCheckBoxStyle}">My Checkbox</CheckBox>

<dxe:TextEdit x:Uid="dxe:TextEdit_1" Grid.Row="1" Grid.Column="1" Width="100" Style="{StaticResource FleetScheduledHoursStyle}" EditValue="{Binding RealValue, Mode=OneWay}" EditMode="InplaceInactive" ToolTipService.ShowDuration="20000" />

<dxe:TextEdit x:Uid="dxe:TextEdit_2" Grid.Row="1" Grid.Column="1" Width="100" Style="{StaticResource FleetScheduledHoursStyle}" EditValue="{Binding RealValue, Mode=OneWay}" EditMode="InplaceInactive" ToolTipService.ShowDuration="20000" />

TickCheckBox绑定到我的视图模型中的一个属性,如下所示:

private bool tickCheckBox;
public bool TickCheckBox
{
    get
    {
        return this.tickCheckBox;
    }
    set
    {
        if (this.TickCheckBox.Equals(value))
        {
               return;
        }
        this.tiketCheckBox = value;
        this.NotifyPropertyChange(() => this.TickCheckBox);
    }
}

当我勾选复选框时,如何将textedit(例如Text_Edit1)之一的属性“ EditMode”更改为“ InplaceActive”?

感谢您的帮助!

wpf data-binding devexpress wpf-controls
1个回答
0
投票

您可以使用IValueConverter

BoolToEditModeConverte.cs

public class BoolToEditModeConverter : IValueConverter
{
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    if (!(value is bool isChecked))
    {
      return Binding.DoNothing;
    }
    return isChecked 
      ? EditMode.InplaceInactive
      : EditMode.None;
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
    throw new NotSupportedException();
  }
}

用法

<Window>
  <Window.Resources>
    <BoolToEditModeConverte x:Key="BoolToEditModeConverte" />
  </Window.Resources>

  <CheckBox x:Name="MyCheckbox" />

  <TextEdit EditMode="{Binding ElementName=MyCheckBox, 
                               Path=IsChecked, 
                               Converter={StaticResource BoolToEditModeConverte}}" />
</Window>
© www.soinside.com 2019 - 2024. All rights reserved.