首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > CAD教程 >

wpf的combobox和datagrid有关问题

2013-11-21 
wpf的combobox和datagrid问题如图。怎么实现combobox 选项改变 datagrid也跟着改变?[解决办法]这个建议使用

wpf的combobox和datagrid问题
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));
                }
            }
        }


可以参照上面的,自己整理下吧,我是简单的写了下,有什么问题,可以再交流~
主要就是MVVM的使用
[解决办法]
那就new datagrid,设置 相关属性,然后stackpanel.children.add(新的grid)

热点排行