C# 将WPF DataGridComboxColumns数据绑定到MVVM不起作用

C# 将WPF DataGridComboxColumns数据绑定到MVVM不起作用,c#,wpf,mvvm,caliburn.micro,C#,Wpf,Mvvm,Caliburn.micro,以下是第2部分中的答案: 1)在组合框中进行选择时,我无法获取要设置的ObservableCollections中的值。 正在使用ViewModel中的列表填充组合框,但未设置值 代码: ViewModel界面(我已调试并连接到ViewModel,视图上的其他控件已正确绑定: ETA:BindableCollection继承自ObservableCollection,它是Caliburn.Micro类型 public interface ICustomSIDViewModel : ISc

以下是第2部分中的答案:

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

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

代码:


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; }
}
公共接口ICustomSIDViewModel:IScreen
{
BindableCollection SidValues1到5{get;set;}
BindableCollection SidValues6through10{get;set;}
IList AvailableSids{get;}
}
公共类价值观
{
公共字符串值1{get;set;}
公共字符串值2{get;set;}
公共字符串值3{get;set;}
公共字符串值4{get;set;}
公共字符串值5{get;set;}
}
2)一旦我解决了这个问题,是否有一种更干净的方法让所有列继承这一组DataGridComboxColumn.ElementStyle和DataGridComboxColumn.EditingElementStyle?

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

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));
    }

您正在绑定集合的一个属性,该属性是组合框的ItemsSource,这是错误的。这就像您应该将单独的属性作为上面的SelectedValue,并将此属性绑定到ComboBox的SelectedValue。在此属性中,您可以获取并设置ComboBox的选定值。我希望这会有所帮助。

对于第一个问题,这是WPF,因此您不需要在绑定上使用Mode=TwoWay,但先试试,以防万一

WPF afaik的默认值是双向的,但SL的默认值不是双向的

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

e、 g



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

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

我仍然很好奇如何让DataGrid与MVVM和Caliburn.Micro一起工作,我尝试了我找到的每一个示例,但无法让combobox选项更新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>


如果我将整个块添加到资源中,组合框部分将变灰,这意味着没有被使用。如果我将样式TargetType=“ComboBox”部分放在xaml顶部的参考资料部分,并设置样式BasedOn=“{staticResourceKey=ComboBoxStle}”(我使用x:Key=“ComboxStyle”命名该样式,我会得到以下错误:“只能基于目标类型为基类型“IFrameworkInputElement”的样式。”不,应该可以-嵌套样式肯定可以-等等,我会检查我的语法可能有点不正确我该如何处理DataGridComboxColumn.ElementStyle和DataGridComboxColumn.EditingElementStyle部分?它们需要一些内部内容。运行时在输出窗口中出错:System.Windows.Data错误:40:BindingExpression path错误:在“对象”“字符串”(HashCode=-1241299750)上找不到“Value1”属性。BindingExpression:Path=Value1;DataItem='String'(HashCode=-1241299750);目标元素为“TextBlockComboBox”(名称=“”);目标属性为“NotTarget”(类型为“object”)我自己很难做到这一点-我对Teleriks RadGrid控件也做了同样的事情,效果很好..但是在数据网格上设置ItemsSource似乎对我不起作用!与其尝试IList,不如尝试ObservableCollection
<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>
<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>