【问题标题】:WPF DataGridComboBoxColumn Binding in MVVM?MVVM中的WPF DataGridComboBoxColumn绑定?
【发布时间】:2014-09-03 14:46:35
【问题描述】:

我的 ViewModel 中有一个 DataGridComboBoxColumn 绑定到 ObservableCollection<String>。此绑定有效,提供 ObservableCollection 的下拉列表:

这是我的 Xaml:

<DataGrid Margin="5" AutoGenerateColumns="False" ItemsSource="{Binding MembershipsCollection}">
   <DataGrid.Columns>
      <DataGridComboBoxColumn Header="Region" Width="SizeToHeader">
        <DataGridComboBoxColumn.ElementStyle>
             <Style>
                 <Setter Property="ComboBox.ItemsSource" Value="{Binding DataContext.RegionShortCodeCollection, RelativeSource={RelativeSource AncestorType={x:Type vm:AccountsViewModel}}}" />
              </Style>
             </DataGridComboBoxColumn.ElementStyle>
          <DataGridComboBoxColumn.EditingElementStyle>
               <Style>
                  <Setter Property="ComboBox.ItemsSource" Value="{Binding DataContext.RegionShortCodeCollection, RelativeSource={RelativeSource AncestorType={x:Type v:AccountsView}}}" />
                </Style>
             </DataGridComboBoxColumn.EditingElementStyle>
           </DataGridComboBoxColumn>
         <DataGridTextColumn Header="ProductCode" Binding="{Binding Path=ProductCode}" Width="SizeToHeader"/>                                                                  
        </DataGrid.Columns>
       </DataGrid>

& 我的 ViewModel 中的 ObservableCollection。这是由构造函数调用的方法填充的:

  private ObservableCollection<String> _RegionShortCodeCollection;
    public ObservableCollection<String> RegionShortCodeCollection
    {
        get { return _RegionShortCodeCollection; }
        set
        {
            _RegionShortCodeCollection = value;
            OnPropertyChanged("RegionShortCodeCollection");
        }
    }

我现在想将单元格的值绑定到数据库中的实际当前值(与图片中的 PRODUCTCODE 相同的ObservableCollection&lt;T&gt;,因为目前这显然是空白的:

有没有办法绑定到同一列的多个属性?如果没有,有没有办法解决这个问题?希望清楚,谢谢。

【问题讨论】:

  • DataGrid 的 ItemsSource 是什么?通常,您会将 ComboBox 的 SelectedItem 绑定到保存此值的 ItemsSource 的属性。
  • 更新了问题 Xaml。如果我尝试绑定到 ItemSource 集合中的项目,我会收到“附加信息:在 AddNew 或 EditItem 事务期间不允许‘DeferRefresh’”异常...
  • 这是什么? RelativeSource={RelativeSource AncestorType={x:Type vm:AccountsViewModel}} 为什么编辑样式会绑定到不同的 AncestorType?
  • 另外,DisplayMemberPath 绑定对我来说似乎很奇怪。您不需要设置此属性,因为您将 ItemsSource 绑定到字符串集合......没有路径。即使你正在绑定它......它不应该是一个集合,而是一个字符串。
  • 谢谢@LeeO。 Xaml 已被编辑。仍然没有运气。尝试绑定到此集合项目时,在 AddNew 或 EditItem 事务异常期间不允许“延迟刷新”。我可以绑定到另一个 ObservableCollection 没问题...

标签: c# wpf xaml mvvm


【解决方案1】:

不确定我是否误解了您的问题,但我认为您不需要 DataGridComboBoxColumn 的样式 - 只需直接使用 ItemsSource 值即可。至于您的问题,您应该能够使用SelectedItemBinding(或SelectedValueBinding,如果适用)将行视图模型属性绑定到所选项目:

 <DataGridComboBoxColumn Header="Region" 
     ItemsSource="{Binding DataContext.RegionShortCodeCollection, RelativeSource={RelativeSource AncestorType={x:Type v:AccountsView}}"
     SelectedItemBinding="{Binding SelectedRegionShortCode,Mode=TwoWay}"
 />

【讨论】:

  • 恐怕此代码的 UI 中没有返回任何内容。我实际上是在尝试查看单元格中的当前值,然后单击下拉列表以获得替代选项列表,我认为这很容易,但与许多 WPF/Xaml 一样,它很快就会变得非常冗长!跨度>
  • 那是因为 DataGridColumn 不是可视化树的一部分,所以它不能像这样进行 RelativeSource 查找。因此需要 ElementStyle & EditingElementStyle。
猜你喜欢
  • 2011-07-27
  • 2012-10-28
  • 2017-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-05
  • 2018-01-22
  • 1970-01-01
相关资源
最近更新 更多