0

您好,我是 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;
    }
}
4

2 回答 2

0
  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/en-us/library/system.componentmodel.inotifypropertychanged.aspx http: //msdn.microsoft.com/en-us/library/system.windows.input.icommand.aspx

于 2013-10-21T15:23:26.223 回答
0

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

于 2013-10-21T14:45:28.153 回答