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

时间:2013-10-11 10:37:03

标签: c# wpf

我正在尝试将文本框绑定到String变量,同时我想将ComboBox绑定到List。

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

    }
}

我似乎无法同时绑定Combobox和Textbox。我已经尝试了很多解决方案。但无论哪种方式,其中只有一个有效。 对此有什么简单的解决方案吗?

3 个答案:

答案 0 :(得分:1)

您的属性应该像这样调用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页面中的示例。

答案 1 :(得分:1)

尝试更改此内容。

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

答案 2 :(得分:0)

  public string Name1
    {
        get { return _name; }
        set
        {
            _name = value;
        }
    }

忘记了OnPropertyChanged("Name1");

另外:

public ObservableCollection<string> ListString;

您的ListString应该是一个Property,而不仅仅是一个字段:

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

最后,我没有看到原因:

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