同时将两个控件绑定到两个源

本文关键字:两个 绑定 控件 | 更新日期: 2023-09-27 18:35:00

我正在尝试将文本框绑定到字符串变量,同时我想将组合框绑定到列表。

<Window 
        x:Class="Assignment2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:validators="clr-namespace:Assignment2"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">
    <Grid>
        <ComboBox  Height="23" HorizontalAlignment="Left" Margin="109,103,0,0" Name="StringComboBox" VerticalAlignment="Top" Width="120" SelectionChanged="SelectionChanged">
            <ComboBox.ItemsSource>
                <Binding Path="ListString" Mode="TwoWay" UpdateSourceTrigger="LostFocus"></Binding>
            </ComboBox.ItemsSource>
        </ComboBox>
        <TextBox Height="23" HorizontalAlignment="Right" Margin="0,51,250,0" Name="inputTextBox" VerticalAlignment="Top" Width="154" LostFocus="inputTextBox_LostFocus">
            <TextBox.Text>
            <Binding Path="Name1" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
                <Binding.ValidationRules>
                    <validators:RequiredFieldValidator ErrorMessage="This field Should not be empty"></validators:RequiredFieldValidator>
                    <validators:OnlyCharacterValidation ErrorMessage="Only characters allowed"></validators:OnlyCharacterValidation>
                </Binding.ValidationRules>
            </Binding>
            </TextBox.Text>
        </TextBox>
    </Grid>
</Window>

代码隐藏是:

public partial class MainWindow : Window, INotifyPropertyChanged
    {
        string _name = "Default Value";
        public ObservableCollection<string> ListString;
        public string Name1
        {
            get { return _name; }
            set
            {
                _name = value;
            }
        }
        public MainWindow()
        {
            ListString = new ObservableCollection<string>();
            ListString.Add("AAA");
            ListString.Add("BBB");
            ListString.Add("CCC");
            ListString.Add("DDD");
            InitializeComponent();
        }
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) 
                handler(this, new PropertyChangedEventArgs(propertyName));
        }
        private void inputTextBox_LostFocus(object sender, RoutedEventArgs e)
        {
            OnPropertyChanged("Name1");
        }
    }
}

我似乎无法同时绑定组合框和文本框。我已经尝试了许多解决方案。但无论哪种方式,这些方法中只有一种有效。有没有简单的解决方案?

同时将两个控件绑定到两个源

属性应像这样调用OnPropertyChanged处理程序:

public string Name1
{
    get { return _name; }
    set
    {
        _name = value;
        OnPropertyChanged("Name1"); 
    }
}
private ObservableCollection<string> listString = new ObservableCollection<string>();
public ObservableCollection<string> ListString
{
    get { return listString ; }
    set
    {
        listString = value;
        OnPropertyChanged("ListString"); 
    }
}

请仔细查看 MSDN 上的 INotifyPropertyChanged Interface 页中的示例。

尝试更改为此内容。

public string Name1
{
    get { return _name; }
    set
    {
        _name = value;
        OnPropertyChanged("Name1");
    }
}
  public string Name1
    {
        get { return _name; }
        set
        {
            _name = value;
        }
    }

忘记OnPropertyChanged("Name1");了?

也:

public ObservableCollection<string> ListString;

你的ListString应该是一个属性,而不仅仅是一个字段:

public ObservableCollection<string> ListString { get; set; }

最后,我看不出原因:

private void inputTextBox_LostFocus(object sender, RoutedEventArgs e)
{
  OnPropertyChanged("Name1");
}