【问题标题】:WPF DateGrid: How to bind a column of ComboBox plus need to enable/disable entries dynamicallyWPF DataGrid:如何绑定一列 ComboBox 加上需要动态启用/禁用条目
【发布时间】:2013-11-27 18:05:54
【问题描述】:

这部分已准备就绪并可以运行:

“如何绑定一列ComboBox”

<DataGridTemplateColumn Header="Bot Plate Thickness">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding SteelThickness, RelativeSource={RelativeSource AncestorType=Window}}" SelectedItem="{Binding BottomPlateThickness, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="ComboBox_SelectionChanged" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

这是我模型的相关部分:

public class GridModel : PropertyChangedBase
{
    private string _BottomPlateThickness;
    public  string  BottomPlateThickness
    {
        get
        {
            return _BottomPlateThickness;
        }
        set
        {
            if (_BottomPlateThickness != value)
            {
                _BottomPlateThickness = value;
                RaisePropertyChanged("BottomPlateThickness");
            }
        }
    }
}

这是另一部分:

public List<string> SteelThickness { get; set; }

SteelThickness = new List<string> { "0.3750", "0.4375", "0.5000", "0.6250", "0.7500", "0.8650", "1.0000" };

如您所见,ComboBox 内容基于静态列表。我读到为了能够打开/关闭,我必须将该列基于ObservableList&lt;object&gt;

TIA。

【问题讨论】:

  • 我不明白......你的意思是,将“BottomPlateThickness”绑定到选定的组合框行?
  • 没错。顺便说一句:它绑定到 COLUMN,而不是行。它运行良好,但我需要添加启用/禁用的功能。
  • 我无法理解您的要求:\
  • 我感觉我的模型中需要一个嵌套类
  • 如果你有Enabled的属性,那你就不能用&lt;ComboBox IsEnabled="{Binding IsEnabled}" ...吗?

标签: c# wpf xaml datagrid


【解决方案1】:

答案很简单...您不能在DataGridTemplateColumn 的特定行中访问ComboBox。为什么这么多人想像 WinForms 一样使用 WPF?... 不是 WinForms。我说了这么多,我开始觉得自己像个传教士... WPF 是一种以数据为中心的语言... 这意味着我们操作 数据对象,而不是 UI 对象。为什么不让您的生活更轻松并握住比喻性的锤子而不是头部

数据的角度考虑这个问题,我们有一个数据对象的集合。一个对象由DataGrid 中的一行表示,因此这些对象的一个​​属性将与相关ComboBoxes 中的选定项目相关。现在,如果您想让您的生活变得非常轻松,您可以在该类中添加一个新的集合属性,您可以将其绑定到 ComboBox.ItemsSource 属性。

现在,要更改每个 ComboBox 中的可能选项,您需要做的就是更改数据绑定对象中该集合属性中的项目。您可以将数据类型中的“未过滤”集合定义为每个实例可以共享的 static 变量:

private static ObservableCollection<string> steelThickness = new 
    ObservableCollection<string> { "0.3750", "0.4375", "0.5000", "0.6250", "0.7500", 
    "0.8650", "1.0000" };

public ObservableCollection<string> ComboBoxItemsSource
{
    get { return new ObservableCollection<string>(
        steelThickness.Where(item => item.MeetsCertainCriteria(item))); }
}

private bool MeetsCertainCriteria(string item)
{
    // return true or false for each item to adjust the members in the collection
    // based on whatever your condition is
    if ( ... ) return true;
    return false;
}

此设置需要注意的唯一一点是,您必须在集合更新时手动调用NotifyPropertyChange("ComboBoxItemsSource"),并且您需要使用OneWay Binding,因为它没有设置器。现在,这不是更容易吗?

【讨论】:

  • "为什么这么多人想像 WinForms 一样使用 WPF?"我是新手。没用过WinForms。 WPF 是我的第一个 Windows GUI 环境。
猜你喜欢
  • 1970-01-01
  • 2014-12-15
  • 1970-01-01
  • 2014-09-04
  • 2010-10-14
  • 2015-10-30
  • 2011-05-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多