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

Changing a property controller based on other controller's property value in wpf

考虑在我的 wpf 应用程序中,我有一个复选框和 2 个文本编辑器,如下所示:

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

如何在勾选复选框时将其中一个文本编辑器(比如 Text_Edit1)的 属性 "EditMode" 更改为 "InplaceActive"?

感谢您的帮助!

您可以使用 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))
    {
      throw new ArgumentException("Converter value must be of type 'bool'");
    }
    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>

因为你使用的是 POCOViewModel,你只需为 TextEdit.EditMode 定义一个 属性 并在 xaml 中绑定,并为 poco 中的 TickCheckBox changed 事件定义一个方法查看模型,像这样:

public partial class MainWindow : Window {
    public MainWindow() {
        InitializeComponent();
        this.DataContext = Vm.Create();
    }
}

[POCOViewModel]
public class Vm {
    public virtual bool TickCheckBox { get; set; } = false;
    public virtual EditMode EditMode { get; set; } = EditMode.InplaceInactive;

    public static Vm Create() => ViewModelSource.Create(() => new Vm());

    protected void OnTickCheckBoxChanged() {
        if (this.TickCheckBox) {
            // or this.EditMode = EditMode.InplaceActive;
            this.EditMode = EditMode.Standalone;
        } else {
            this.EditMode = EditMode.InplaceInactive;
        }
    }
}

和xaml:

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

    <dxe:TextEdit
        x:Uid="dxe:TextEdit_1"
        EditMode="{Binding EditMode}"
        EditValue="{Binding RealValue, Mode=OneWay}"
        ToolTipService.ShowDuration="20000" />
    <dxe:TextEdit
        x:Uid="dxe:TextEdit_2"
        EditMode="Standalone"
        EditValue="{Binding RealValue, Mode=OneWay}"
        ToolTipService.ShowDuration="20000" />
</StackPanel>