【问题标题】:Databinding WPF DatagridComboBoxColumns to MVVM not working将 WPF DatagridComboBoxColumns 数据绑定到 MVVM 不起作用
【发布时间】:2012-08-10 16:57:53
【问题描述】:

遵循这 2 个中的答案:

Using WPF DataGridComboBoxColumn with MVVM - Binding to Property in ViewModel

Binding a WPF DataGridComboBoxColumn with MVVM

1) 在组合框中进行选择时,我无法设置 ObservableCollections 中的值。

组合框正在使用 ViewModel 中的列表填充,但未设置值。

代码:

<DataGrid ItemsSource="{Binding SidValues1through5, Mode=TwoWay}"                                  
     AutoGenerateColumns="False"
     Grid.Row="1"   
     Margin="5"
     VerticalAlignment="Top"
     HorizontalAlignment="Center">
     <DataGrid.Columns>
           <DataGridComboBoxColumn Header="1"
                                   Width="100"
                                   SelectedValueBinding="{Binding Value1}"
                                   SelectedValuePath="Value1">
                                    <DataGridComboBoxColumn.ElementStyle>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="ItemsSource"
                                                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AvailableSids}" />
                                        </Style>
                                    </DataGridComboBoxColumn.ElementStyle>
                                    <DataGridComboBoxColumn.EditingElementStyle>
                                        <Style TargetType="ComboBox">
                                            <Setter Property="ItemsSource"
                                                    Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AvailableSids}" />
                                        </Style>
                                    </DataGridComboBoxColumn.EditingElementStyle>
                                </DataGridComboBoxColumn>

ViewModel 接口(我已经调试过并且连接到ViewModel,视图上的其他控件绑定正确:

ETA:BindableCollection 继承自 ObservableCollection,它是一个 Caliburn.Micro 类型。

public interface ICustomSIDViewModel : IScreen
{
    BindableCollection<SidValues> SidValues1through5 { get; set; }
    BindableCollection<SidValues> SidValues6through10 { get; set; }

    IList<string> AvailableSids { get; }
}

public class SidValues
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
    public string Value3 { get; set; }
    public string Value4 { get; set; }
    public string Value5 { get; set; }
}

2) 一旦我解决了这个问题,有没有一种更简洁的方法让所有列都继承这一组 DataGridComboBoxColumn.ElementStyle 和 DataGridComboBoxColumn.EditingElementStyle?

我问的原因是有 10 列都有相同的组合框列表。

【问题讨论】:

    标签: c# wpf mvvm caliburn.micro


    【解决方案1】:

    对于第一个问题 - 它是 WPF,因此您不需要在绑定上使用 Mode=TwoWay,但为了以防万一,请尝试一下。

    WPF afaik 的默认值是 TwoWay,但不是 SL。还是试试吧。

    对于第二个问题,只需在资源字典中声明嵌套样式即可。嵌套样式适用于目标控件的子元素

    例如

    <Style x:Key="DataGridComboBoxStyle" TargetType="DataGrid">
        <!-- Nested -->
        <Style.Resources>
                <Style TargetType="ComboBox">
                    <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.AvailableSids}" />
                </Style>
        </Style.Resources>
    </Style>
    

    您还可以将样式应用于整个控件并将该样式嵌套在该样式中:)

    【讨论】:

    • 如果我将整个块添加到资源中,ComboBox 部分将显示为灰色,这意味着未使用。如果我只将 Style TargetType="ComboBox" 部分放在 xaml 顶部的资源部分并设置 Style BasedOn="{StaticResource ResourceKey=ComboBoxStle}" (我用 x:Key="ComboBoxStyle" 命名样式我收到以下错误:
    • "只能基于目标类型为 'IFrameworkInputElement' 的样式。"
    • 不应该工作 - 嵌套样式肯定工作 - 等一下,我会检查一下我的可能语法有点不对
    • 如何处理 DataGridComboBoxColumn.ElementStyle 和 DataGridComboBoxColumn.EditingElementStyle 部分?他们需要内在的东西。
    【解决方案2】:
    SelectedValueBinding="{Binding SelectedValue, Mode=TwoWay}"
                                           SelectedValuePath="Value1">
    
    private string selectedValue;
        public string SelectedValue 
        {
            get
            {
                return selectedValue;
            }
            set
            {
                selectedValue = value;
                Notify("SelectedValue");
            } 
        }
    
        private void Notify(string propName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
        }
    

    您正在绑定 Collection 的属性之一,即您的 ComboBox 的 ItemsSource ,这是错误的。应该像上面的 SelectedValue 一样具有单独的属性,并将此属性绑定到 ComboBox 的 SelectedValue。在此属性中您可以获得并设置 ComboBox 的 Selected 值。我希望这会有所帮助。

    【讨论】:

    • 运行时输出窗口出错:System.Windows.Data 错误:40:BindingExpression 路径错误:在“对象”“字符串”(HashCode=-1241299750)上找不到“Value1”属性。绑定表达式:路径=值1; DataItem='String' (HashCode=-1241299750);目标元素是'TextBlockComboBox'(名称='');目标属性是“NoTarget”(类型“对象”)
    • 我很难让这个自己工作 - 我用 Teleriks RadGrid 控件做同样的事情,它工作正常.. 但在 DataGrid 上设置 ItemsSource 似乎不起作用我!
    • 代替 IList 尝试 ObservableCollection
    【解决方案3】:

    我最终必须使用一个内部带有 GridView 的 ListView 来工作并继续该项目。不完全一样,但看起来很相似。

    我仍然对如何让 DataGrid 真正与 MVVM 和 Caliburn.Micro 一起工作感到好奇,我尝试了我找到的每个示例,但无法获得组合框选择来更新 VM 上的任何内容。

    这是我的解决方案:

    <ListView.View>
        <GridView>                                    
            <GridViewColumn Header="1"
                           Width="100">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <ComboBox ItemsSource="{Binding Path=DataContext.AvailableSids,
                                            RelativeSource={RelativeSource FindAncestor, 
                                            AncestorType={x:Type UserControl}}}"
                                  SelectedItem="{Binding Path=Value1, Mode=TwoWay, 
                                                  UpdateSourceTrigger=PropertyChanged}" />
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>  
    </ListView.View>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-11
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      相关资源
      最近更新 更多