1

按照这2个答案:

将 WPF DataGridComboBoxColumn 与 MVVM 结合使用 - 绑定到 ViewModel 中的属性

使用 MVVM 绑定 WPF DataGridComboBoxColumn

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 列都有相同的组合框列表。

4

3 回答 3

0
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 的选定值。我希望这会有所帮助。

于 2012-08-10T17:05:52.723 回答
0

对于第一个问题 - 它是 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>

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

于 2012-08-10T17:07:05.453 回答
0

我最终不得不去工作和继续这个项目的是一个 ListView,里面有一个 GridView。不完全一样,但看起来很相似。

我仍然对如何让 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>
于 2012-08-11T18:44:10.000 回答