【问题标题】:How to bind ComboBox found inside of a nested in DataGrid如何绑定在 DataGrid 中嵌套的 ComboBox
【发布时间】:2015-06-23 15:36:29
【问题描述】:

我正在尝试同时学习 WPF 和 MVVM,并且需要一些有关绑定的帮助。我仍然试图让我的头脑围绕将控件绑定到类的属性。

这是我的故障视图模型:

public class Malfunctions : ViewModelBase {
       public ObservableCollection<Model.PartMalfunction> AllPartMalfunctions {
            get;
            private set;
        }
       public ObservableCollection<Model.Part> AllParts {
        get;
        private set;
    }
}

这里是 Model.PartMalfunction.cs:

public class PartMalfunction{
        public ObservableCollection<Model.PartSerial> AllPartSerials {
            get;
            set;
        }
    }

这里是 Model.Part.cs:

public class Part {
        public string Label { get; set; }
        public string Value { get; set; }
    }

我有一个绑定到 Malfunctions ViewModel 中的 AllPartMalfunctions ObservableCollection 的 DataGrid。这个绑定很好用。

我在 RowDetailsTemplate 中嵌套了另一个 DataGrid,它绑定到 PartMalfunction 模型中的 AllPartSerials。这个绑定也很好用。

我的问题在于嵌套 DataGrid 内的组合框。我想将此组合框绑定到 Malfunctions ViewModel 中的 AllParts ObservableCollection。我该怎么做?

<DataGrid ItemsSource="{Binding AllPartMalfunctions}" AutoGenerateColumns="False" Width="Auto"
                        RowDetailsVisibilityMode="Visible">
    <DataGrid.Columns>
    <!--removed for brevity-->
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
    <DataTemplate>
        <DataGrid ItemsSource="{Binding AllPartSerials }" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Name="cboPart" VerticalAlignment="Center" ItemsSource="{Binding AllParts}" DisplayMemberPath="Label" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

【问题讨论】:

  • 包含绑定到 AllPartMalFunctions 的 DataGrid 的元素是什么?
  • @Markus - 如果我理解您的问题,那么您是在问我 XAML 中的控件层次结构,对吗?它先是 DockingPanel,然后是 StackPanel,然后是第一个 DataGrid。
  • 我发布的答案应该有效。我有一些类似的代码将绑定路径设置为包含 DataGrid 的元素中的命令。我询问了您的层次结构,以便我了解与故障相关的内容。

标签: wpf data-binding combobox


【解决方案1】:

我的案例也采用了类似的方法。我实现了一个具有不同视图模型结构的模型。

public class PartMalfunction{
    public ObservableCollection<Model.PartSerial> AllPartSerials {
        get;
        set;
    }
    public ObservableCollection<Model.Part> AllParts {
        get { return SomeStaticClass.AllParts; }
    }
}

即使在 DataTemplate 中,绑定也很自然。希望这适合您的域。

【讨论】:

【解决方案2】:

为绑定到故障的元素指定名称并使用相对路径进行绑定,如下面包含 ComboBox 的行所示。对于我的示例,我假设 StackPanel 包含 DataGrid,并且 StackPanel 绑定到容器视图模型的 Malfunctions 属性。

<StackPanel x:Name="MalfunctionsGrid" DataContext={Binding Malfunctions}" Orientation="Vertical">
...
    <DataGrid ItemsSource="{Binding AllPartMalfunctions}" AutoGenerateColumns="False" Width="Auto"
                    RowDetailsVisibilityMode="Visible">
    ...
    ...
        <ComboBox Name="cboPart" VerticalAlignment="Center" ItemsSource="{Binding Path=DataContext.AllParts, ElementName=MalfunctionsGrid}" DisplayMemberPath="Label" />
    ...
    ...

【讨论】:

    猜你喜欢
    • 2014-09-12
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2021-03-29
    • 1970-01-01
    • 2011-02-28
    • 2021-07-07
    相关资源
    最近更新 更多