wpf的combobox和datagrid问题
如图。
怎么实现combobox 选项改变 datagrid也跟着改变?
[解决办法]
这个建议使用MVVM绑定,然后在Comobox的SelectedItem绑定的属性上的Set事件中
<ComboBox SelectedItem="{Binding SelectedData}" ItemsSource="{Binding ComoboxData}"></ComboBox>
<ListView ItemsSource="{Binding GridData}">
<ListView.View>
<GridView>
<GridViewColumn Header="内设机构名称" DisplayMemberBinding="{Binding}"></GridViewColumn>
</GridView>
</ListView.View>
</ListView>
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
//////////////////
public class MainWindowViewModel : INotifyPropertyChanged
{
private string _SelectedData;
public string SelectedData
{
get { return _SelectedData; }
set
{
_SelectedData = value;
OnPropertyChanged("SelectedData");
}
}
private ObservableCollection<string> _ComoboxSource;
public ObservableCollection<string> ComoboxSource
{
get { return _ComoboxSource; }
set
{
_ComoboxSource = value;
OnPropertyChanged("ComoboxSource");
}
}
private ObservableCollection<string> _GridData;
/// <summary>
/// GridData
/// </summary>
public ObservableCollection<string> GridData
{
get { return _GridData; }
set
{
_GridData = value;
OnPropertyChanged("GridData");
}
}
private void RefreshGridData()
{
GridData.Clear();
//GridData.Add();......
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
}