繁体   English   中英

数据绑定WPF XAML

[英]Data binding WPF XAML

您好,我是WPF的新手,我不确定如何进行数据绑定以及后面的代码来启用和禁用我的文本框和按钮。

如果您可以在下面的示例中告诉我如何使其工作,它将对我的项目有所帮助。

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#

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

您可以将文本框和按钮的IsEnabe属性绑定到Combox的SelectedIndex属性,而无需响应ComboBoxA_SelectionChanged事件。 为了让SelectedIndex更改按钮和文本框的IsEnabe,您需要在绑定中使用一个对流器,例如:

  1. 编写如下的类,如下所示

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

    }

编写另一个实现ICommand的类,如下所示。 这是为了处理按钮单击事件

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

    public event EventHandler CanExecuteChanged;

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

在下面更改您的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}"/>

在Xmal.cs中,将数据上下文设置为beolw

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

    }

请参考以下链接,以了解为什么必须使用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