Wpf 多个文本框,但属性相同

Wpf 多个文本框,但属性相同,wpf,xaml,data-binding,textbox,Wpf,Xaml,Data Binding,Textbox,我有选项卡A上的文本框A和选项卡B上的文本框B。我将它们数据绑定到同一个属性。我使用相同的绑定: <TextBox> <TextBox.Text> <Binding Path=ValueA UpdateSourceTrigger="LostFocus"> <Binding.ValidationRules> <local:MyValidationRules /> &l

我有选项卡A上的文本框A和选项卡B上的文本框B。我将它们数据绑定到同一个属性。我使用相同的绑定:

<TextBox>
    <TextBox.Text>
      <Binding Path=ValueA UpdateSourceTrigger="LostFocus">
        <Binding.ValidationRules>
          <local:MyValidationRules />
        </Binding.ValidationRules>
      </Binding>
    </TextBox.Text>
</TextBox>

但是有一个问题。如果文本框A中的值无效,则不会更新源中的属性。但是如果用户切换到选项卡b,它看起来会找到。基本上,验证规则阻止更新更改源


我试图将文本框b绑定到文本框a。问题是验证规则似乎只适用于文本框a,而不适用于文本框b。

从设计角度来看,如果用户在文本框a中输入了无效值,您不希望在文本框b上显示它,因为它是无效的。如果我是你,我会拒绝用户访问选项卡B,直到文本框A有效

以下是一个例子:

视图:

以及ViewModel中的逻辑:

public class MainWindowViewModel : NotificationObject
{
    private string myTextProperty;

    public MainWindowViewModel()
    {
        TabSelectionChangedCommand = new DelegateCommand<TabSelectionParameters>(parameters =>
            {
                if (parameters.InputText == string.Empty) // Here goes the validation of the user input to TextBoxA
                {
                    // Deny selection of tab B by returning the selection index to Tab A
                    parameters.MainTab.SelectedIndex = 0;
                }
            });
    }

    public DelegateCommand<TabSelectionParameters> TabSelectionChangedCommand { get; set; }

    public string MyTextProperty
    {
        get
        {
            return myTextProperty;
        }
        set
        {
            myTextProperty = value;
            RaisePropertyChanged(() => MyTextProperty);
        }
    }
}
公共类MainWindowViewModel:NotificationObject { 私有字符串myTextProperty; 公共主窗口视图模型() { 选项卡SelectionChangedCommand=new DelegateCommand(参数=> { if(parameters.InputText==string.Empty)//下面验证用户对TextBoxA的输入 { //通过将选择索引返回到选项卡A来拒绝选项卡B的选择 parameters.MainTab.SelectedIndex=0; } }); } 公共DelegateCommand选项卡SelectionChangedCommand{get;set;} 公共字符串MyTextProperty { 得到 { 返回myTextProperty; } 设置 { myTextProperty=值; RaisePropertyChanged(()=>MyTextProperty); } } }
希望这对设计有所帮助,如果用户在文本框A中输入了无效值,您不希望在文本框B上显示它,因为它是无效的。如果我是你,我会拒绝用户访问选项卡B,直到文本框A有效。使用
IDataErrorInfo
@Omribitan,而不是
ValidationRule
,谢谢。“你的建议确实解决了我的问题。”马丁用一个代码示例更新了我的答案,说明了如何做这样的事情。祝你好运:)
public class TabSelectionConverter : IMultiValueConverter
{
    #region IMultiValueConverter Members

    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return new TabSelectionParameters 
        { 
            MainTab = values[0] as TabControl,
            InputText = values[1] as string 
        };
    }

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

    #endregion
}

public class TabSelectionParameters
{
    public TabControl MainTab { get; set; }

    public string InputText { get; set; }
}
public class MainWindowViewModel : NotificationObject
{
    private string myTextProperty;

    public MainWindowViewModel()
    {
        TabSelectionChangedCommand = new DelegateCommand<TabSelectionParameters>(parameters =>
            {
                if (parameters.InputText == string.Empty) // Here goes the validation of the user input to TextBoxA
                {
                    // Deny selection of tab B by returning the selection index to Tab A
                    parameters.MainTab.SelectedIndex = 0;
                }
            });
    }

    public DelegateCommand<TabSelectionParameters> TabSelectionChangedCommand { get; set; }

    public string MyTextProperty
    {
        get
        {
            return myTextProperty;
        }
        set
        {
            myTextProperty = value;
            RaisePropertyChanged(() => MyTextProperty);
        }
    }
}