ComboBox.SelectedItem 绑定

发布于 2024-11-26 18:20:08 字数 3728 浏览 0 评论 0原文

我的 DataGrid 中有此列/代码:

<sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Province/State">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=SelectedProvince.ProvinceName, Mode=OneWay}"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                    <sdk:DataG  ridTemplateColumn.CellEditingTemplate>                        
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"
                                      SelectedItem="{Binding Path=SelectedProvince, Mode=TwoWay}"
                                      DisplayMemberPath="ProvinceName" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellEditingTemplate>
                </sdk:DataGridTemplateColumn>

那么这是后面的代码(我已经删除了不相关的代码):

            public class BatchSeedingAddressVM : ViewModelBase
            {
                public BatchSeedingAddressVM()
                {
                    _saveAddressButtonCommand = new RelayCommand(SaveAddressButtonCommand_OnExecute);

                    CreateJurisdictionList();
                }

                public BatchSeedingAddressVM(int? batchSeedingAddressOID, string address1, string address2, string city, string postalCode, string province2Code)
                {
                    _saveAddressButtonCommand = new RelayCommand(SaveAddressButtonCommand_OnExecute);
                    CreateJurisdictionList();

                    BatchSeedingAddressOID = batchSeedingAddressOID;
                    Address1 = address1;
                    Address2 = address2;
                    City = city;
                    PostalCode = postalCode;
                    //SelectedProvince2Code = province2Code;
                    SelectedProvince = _provinceList.Where(x => x.Province2Code == province2Code).FirstOrDefault();
                }


                private ObservableCollection<Province> _provinceList = new ObservableCollection<Province>();
                public ObservableCollection<Province> ProvinceList
                {
                    get
                    {
                        return _provinceList;
                    }
                    set
                    {
                        if (_provinceList != value)
                        {
                            _provinceList = value;
                            RaisePropertyChanged("ProvinceList");
                        }
                    }
                }

        private Province _selectedProvince;
        [Display(Name = "Province")]
        public Province SelectedProvince
        {
            get
            {
                return _selectedProvince;
            }
            set
            {
                if (_selectedProvince != value && value != null)
                {
                    _selectedProvince = value;                            
                    RaisePropertyChanged("SelectedProvince");                        
                }
            }
        }
        }

这是问题:DataGrid 单元格有 2 个模板:CellTemplate 和 CellEditingTemplate。当 CellTemplate 处于活动状态时,其中的文本框会按计划选择 SelectedProvince 并显示省份名称。问题是,当 CellEditingTemplate 变为活动状态时,其中的 ComboBox 不会拾取(默认)SelectedItem 值并显示一个空框。

我有什么遗漏的吗?必须如何设置绑定才能在 CellEditingTemplate 的组合框中设置默认的 SelectedItem?

提前非常感谢!

I have this column/code in my DataGrid:

<sdk:DataGridTemplateColumn CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Province/State">
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=SelectedProvince.ProvinceName, Mode=OneWay}"/>
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                    <sdk:DataG  ridTemplateColumn.CellEditingTemplate>                        
                        <DataTemplate>
                            <ComboBox ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"
                                      SelectedItem="{Binding Path=SelectedProvince, Mode=TwoWay}"
                                      DisplayMemberPath="ProvinceName" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellEditingTemplate>
                </sdk:DataGridTemplateColumn>

Then this is the code behind(I have cut out the unrelated code):

            public class BatchSeedingAddressVM : ViewModelBase
            {
                public BatchSeedingAddressVM()
                {
                    _saveAddressButtonCommand = new RelayCommand(SaveAddressButtonCommand_OnExecute);

                    CreateJurisdictionList();
                }

                public BatchSeedingAddressVM(int? batchSeedingAddressOID, string address1, string address2, string city, string postalCode, string province2Code)
                {
                    _saveAddressButtonCommand = new RelayCommand(SaveAddressButtonCommand_OnExecute);
                    CreateJurisdictionList();

                    BatchSeedingAddressOID = batchSeedingAddressOID;
                    Address1 = address1;
                    Address2 = address2;
                    City = city;
                    PostalCode = postalCode;
                    //SelectedProvince2Code = province2Code;
                    SelectedProvince = _provinceList.Where(x => x.Province2Code == province2Code).FirstOrDefault();
                }


                private ObservableCollection<Province> _provinceList = new ObservableCollection<Province>();
                public ObservableCollection<Province> ProvinceList
                {
                    get
                    {
                        return _provinceList;
                    }
                    set
                    {
                        if (_provinceList != value)
                        {
                            _provinceList = value;
                            RaisePropertyChanged("ProvinceList");
                        }
                    }
                }

        private Province _selectedProvince;
        [Display(Name = "Province")]
        public Province SelectedProvince
        {
            get
            {
                return _selectedProvince;
            }
            set
            {
                if (_selectedProvince != value && value != null)
                {
                    _selectedProvince = value;                            
                    RaisePropertyChanged("SelectedProvince");                        
                }
            }
        }
        }

Here is the issue: the DataGrid cell has 2 templates: CellTemplate and CellEditingTemplate. When the CellTemplate is active the textbox in it picks up the SelectedProvince as planned and displays the name of the province. The problem is that when CellEditingTemplate becomes active the ComboBox in it does not pick up the (default)SelectedItem value and displays an empty box.

Is there something I am missing? How the binding has to be setup so that it would be possible to set the default SelectedItem in the combobox in CellEditingTemplate?

Thanks much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-03 18:20:08

我认为 ItemsSource 的 TwoWay 绑定可能是问题所在。

ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"

我建议根据您的设计将其更改为 OneTimeOneWay

I think TwoWay binding for ItemsSource can be the problem.

ItemsSource="{Binding Path=ProvinceList, Mode=TwoWay}"

I suggest to change it to OneTime or OneWay depending on your design.

小嗷兮 2024-12-03 18:20:08

我可能是错的,但我认为如果您使用 SelectedValuePath,则需要使用 SelectedValue 而不是 SelectedItem,因此更改此:

SelectedItem="{Binding Path=SelectedProvince, Mode=TwoWay}"

对此:

SelectedValue="{Binding Path=SelectedProvince, Mode=TwoWay}"

I might be wrong, but I think that if you're using SelectedValuePath, you need to use SelectedValue instead of SelectedItem, so change this:

SelectedItem="{Binding Path=SelectedProvince, Mode=TwoWay}"

to this:

SelectedValue="{Binding Path=SelectedProvince, Mode=TwoWay}"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文