简体   繁体   English

数据绑定WPF XAML

[英]Data binding WPF XAML

Hello I am new to WPF and I am not sure how to do the data binding and the code behind to get my textbox and button to be enabled and disabled. 您好,我是WPF的新手,我不确定如何进行数据绑定以及后面的代码来启用和禁用我的文本框和按钮。

If you could show me how to get it to work in the example below it would help me out in my project. 如果您可以在下面的示例中告诉我如何使其工作,它将对我的项目有所帮助。

XAML XAML

<ComboBox Name="ComboBoxA"                  
          Margin="5"  
          SelectedIndex="0" SelectionChanged="ComboBoxA_SelectionChanged"  >
    <ComboBox.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel Orientation="Vertical" Height="Auto" Margin="5" />
        </ItemsPanelTemplate>
    </ComboBox.ItemsPanel>
    <ComboBoxItem Content="Option1" Width="72"  />
    <ComboBoxItem Content="Option2" Width="72" />
    <ComboBoxItem Content="Option3" Width="72" />
</ComboBox>

<TextBox Name="TextBoxA"
         Margin="5" 
         Width="200"   
         IsEnabled="{Binding TextBoxEnabled}" />

<Button Name="ButtonA"
        Content="Next" 
        HorizontalAlignment="left"                
        Margin="5"                 
        IsEnabled="{Binding ButtonEnabled} />

C# C#

private void ComboBoxA_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    TextBoxA = new TextBox();
    ButtonA = new Button();

    if (ComboBoxAfterProcessing.SelectedIndex == 0)
    {                    
        TextBoxA.IsEnabled = false;                    
        ButtonA.IsEnabled = false;
    }
    else if (ComboBoxAfterProcessing.SelectedIndex == 1)
    {                   
        TextBoxA.IsEnabled = true;
        ButtonA.IsEnabled = true;
    }
    else if (ComboBoxAfterProcessing.SelectedIndex == 2)
    {
        TextBoxA.IsEnabled = true;
        ButtonA.IsEnabled = true;
    }
}

you can binding your textbox and button's IsEnabe Property to the Combox's SelectedIndex Property and you neednot response to ComboBoxA_SelectionChanged Event. 您可以将文本框和按钮的IsEnabe属性绑定到Combox的SelectedIndex属性,而无需响应ComboBoxA_SelectionChanged事件。 in order to let the SelectedIndex change your button and textBox's IsEnabe you need a convetor in your binding for example: 为了让SelectedIndex更改按钮和文本框的IsEnabe,您需要在绑定中使用一个对流器,例如:

  1. Write a class as follows which shall look like below 编写如下的类,如下所示

     public class ViewMole:INotifyPropertyChanged { public ViewMole() { ListValues = new List<string>() { "Option1", "Option2", "Option3", "Option4", "Option5" }; } public ICommand Click { get { return new RelayCommand(); } } public List<string> ListValues { get; set; } string a; public string A { get { return a; } set { a = value; RaisePropertyChanged("A"); } } int index=0; public int SelectedIndex { get { return index; } set { index = value; RaisePropertyChanged("SelectedIndex"); A = ListValues[index]; if (index == 0) { IsEnabled = false; } else { IsEnabled = true; } } } bool isEnabled; public bool IsEnabled { get { return isEnabled; } set { isEnabled = value; RaisePropertyChanged("IsEnabled"); } } public event PropertyChangedEventHandler PropertyChanged; private void RaisePropertyChanged(string propertyName) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } 

    } }

Write another class which implements ICommand as below. 编写另一个实现ICommand的类,如下所示。 This is to handel button click events 这是为了处理按钮单击事件

     public class RelayCommand : ICommand
    {
    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
        MessageBox.Show("Button cliked");
    }
}

Change your Xaml below 在下面更改您的Xaml

    <ComboBox Name="ComboBoxA"  SelectedIndex="{Binding SelectedIndex, UpdateSourceTrigger=PropertyChanged}" ItemsSource="{Binding ListValues}"/>

    <TextBox Name="TextBoxA"  Margin="5"    Width="200"   Text="{Binding A}" IsEnabled="{Binding IsEnabled}"/>

    <Button Name="ButtonA"   Content="Next"   HorizontalAlignment="left" Margin="5"    Command="{Binding Click}" IsEnabled="{Binding IsEnabled}"/>

In Xmal.cs set the data Context as beolw 在Xmal.cs中,将数据上下文设置为beolw

    public MainWindow()
    {
        InitializeComponent();
        DataContext = new ViewMole();

    }

Refer the below links to understand why INotifyPropertyChanged and ICommand has to be used http://wpftutorial.net/INotifyPropertyChanged.html http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx http://msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx 请参考以下链接,以了解为什么必须使用INotifyPropertyChanged和ICommand http://wpftutorial.net/INotifyPropertyChanged.html http://msdn.microsoft.com/zh-CN/library/system.componentmodel.inotifypropertychanged.aspx http: //msdn.microsoft.com/zh-CN/library/system.windows.input.icommand.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM