C# 数据绑定WPF XAML
您好,我是WPF的新手,我不知道如何进行数据绑定和代码隐藏,以便启用和禁用我的文本框和按钮 如果你能告诉我如何让它在下面的例子工作,它将帮助我在我的项目 XAMLC# 数据绑定WPF XAML,c#,wpf,xaml,C#,Wpf,Xaml,您好,我是WPF的新手,我不知道如何进行数据绑定和代码隐藏,以便启用和禁用我的文本框和按钮 如果你能告诉我如何让它在下面的例子工作,它将帮助我在我的项目 XAML 您可以将文本框和按钮的IsEnable属性绑定到Combox的SelectedIndex 属性,您无需响应ComboBoxA_SelectionChanged事件。为了让 选择“索引”更改按钮和文本框的设置如果您的绑定中需要转换器 例如: 写一个如下所示的类 public class ViewMole:INotifyPropertyC
您可以将文本框和按钮的IsEnable属性绑定到Combox的SelectedIndex 属性,您无需响应ComboBoxA_SelectionChanged事件。为了让 选择“索引”更改按钮和文本框的设置如果您的绑定中需要转换器 例如:
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));
}
}
在下面更改您的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}"/>
请参阅以下链接以了解为什么必须使用INotifyPropertyChanged和ICommand
通过谷歌搜索找到“”“你更应该浏览msdn,msdn详细解释了数据绑定,并提供了一些简单的示例,正如David建议的那样,浏览一些教程。发布代码后,您可以使用“代码隐藏”来绕过绑定。数据绑定需要设置用户控件的控件父控件或类似控件的数据上下文。如果视图模型发送正确的IPropertyNotify通知,则控件将更改。另外,Kobe下面的回答是另一种实现绑定的方法,不需要直接在控件实例上设置代码隐藏。我已经阅读了上面的页面,但遗憾的是,在我的示例中,我仍然无法让它工作。我尝试将INotifyPropertyChanged包含到“public partial class AddNewEvent:Window,INotifyPropertyChanged”中,并遵循其实现,但失败了。感谢您向我展示如何实现该示例,这些链接对我很有帮助,我将仔细阅读。
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));
}
}
public class RelayCommand : ICommand
{
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
MessageBox.Show("Button cliked");
}
}
<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}"/>
public MainWindow()
{
InitializeComponent();
DataContext = new ViewMole();
}