【问题标题】:How can I bind WPF DataGridComboBoxColumn to ViewModel Property?如何将 WPF DataGridComboBoxColumn 绑定到 ViewModel 属性?
【发布时间】:2015-11-24 02:25:43
【问题描述】:

我的视图中有一个 WPF DataGrid,其中有一列我希望能够使用组合框进行编辑。这样做,我在我的视图模型上创建一个属性,如下所示:

public List<EnumeradorWCFModel> TiposCarga { get; set; }

使用 WCF 服务正确填充属性。现在这是我的 DataGrid 定义:

<DataGrid ItemsSource="{Binding Path=TarifarioSel.TarifariosDet}" 
              IsReadOnly="False" AutoGenerateColumns="False">
        <DataGrid.Columns>                
            <DataGridComboBoxColumn Width="200" Header="Tipo Carga" ItemsSource="{Binding Path=TiposCarga}" 
                SelectedValueBinding="{Binding Path=ID_TipoCarga}"  DisplayMemberPath="Descripcion" SelectedValuePath="ID"/>                
        </DataGrid.Columns>

我也试试这个,没有运气:

<DataGrid ItemsSource="{Binding Path=TarifarioSel.TarifariosDet}" 
              IsReadOnly="False" AutoGenerateColumns="False">
        <DataGrid.Columns>                
            <DataGridComboBoxColumn Width="200" Header="Tipo Carga" ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type UserControl}}, Path=DataContext.TiposCarga}" 
                SelectedValueBinding="{Binding Path=ID_TipoCarga}"  DisplayMemberPath="Descripcion" SelectedValuePath="ID"/>                
        </DataGrid.Columns>

问题是如何将组合框列的 ItemsSource 属性绑定到我的视图模型中的属性。

请帮忙!谢谢!

【问题讨论】:

  • 乍一看你的绑定似乎不错,(只有第二个)。您是否尝试过用 ObservableCollection 替换列表,还是在输出中出现绑定错误?
  • 谢谢@Esh,自己找。我发布了答案。

标签: c# wpf mvvm datagrid


【解决方案1】:

好的。自己找,但不知道为什么一定要做成这样。如果有人需要,我会发布答案。

<DataGrid ItemsSource="{Binding Path=TarifarioSel.TarifariosDet}" 
              IsReadOnly="False" AutoGenerateColumns="False">
        <DataGrid.Columns>                                
            <DataGridComboBoxColumn Width="200" Header="Tipo Carga" 
                SelectedValueBinding="{Binding Path=ID_TipoCarga}"  DisplayMemberPath="Descripcion" SelectedValuePath="ID">
                <DataGridComboBoxColumn.ElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TiposCarga}"/>
                    </Style>
                </DataGridComboBoxColumn.ElementStyle>
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.TiposCarga}"/>
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>

【讨论】:

  • 必须这样做,因为放置 ComboBox 的行具有不同的 DataContext。该行的 DataContext 是 ItemsSource 的特定元素。因此您不能绑定到视图模型的属性。
  • 坦克。我现在如果您看到我的第二个选项也应该工作,那么数据上下文是不同的,但它没有。
猜你喜欢
  • 2011-02-01
  • 2011-06-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-26
  • 2021-10-18
相关资源
最近更新 更多