wpf的combobox和datagrid问题

james362569870 2013-11-16 08:42:52



如图。
怎么实现combobox 选项改变 datagrid也跟着改变?
...全文
98 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
宇峰科技 2013-11-21
  • 打赏
  • 举报
回复
那就new datagrid,设置 相关属性,然后stackpanel.children.add(新的grid)
james362569870 2013-11-16
  • 打赏
  • 举报
回复
你好,谢谢你的用心回复。 可能我的意思没有表达清楚,不好意思。。。 其实我的意思是,Combobox里面的选项改变,显示新的DataGrid。。 比如Combobox里选中单位机构情况,出现一个DataGrid。。。选中客户情况,出现一个新的DataGrid。而不只是DataGrid里的选项改变。。 这样解释希望你能懂,期待你的回复,谢谢..
小猪八Q 2013-11-16
  • 打赏
  • 举报
回复
这个建议使用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的使用

8,736

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧