Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 数据网格内的绑定组合框_C#_Wpf - Fatal编程技术网

C# 数据网格内的绑定组合框

C# 数据网格内的绑定组合框,c#,wpf,C#,Wpf,我有一个DataGrid,里面有3列:Name-DataGridTextColumn、Value-DataGridTextColumn、ParamJson-DataGridTemplateColumn和combobox,还有一个ParameterGrid的ICollectionView作为源 有一个ViewModel提供ParameterGrid的ICollectionView。ParameterGrid包含4个参数:名称、值、ParamJson、SelectedParamJson 我需要处理D

我有一个DataGrid,里面有3列:Name-DataGridTextColumn、Value-DataGridTextColumn、ParamJson-DataGridTemplateColumn和combobox,还有一个ParameterGrid的ICollectionView作为源

有一个ViewModel提供ParameterGrid的ICollectionView。ParameterGrid包含4个参数:名称、值、ParamJson、SelectedParamJson

我需要处理DataGrid的所有行,因此我使用获取所有DataGrid的命令作为命令参数,然后迭代DataGrid.Items(我可以直接使用ICollectionView?)

问题是如何将DataGrid中Combobox中的SelectedItem正确绑定到SelectedParamJson

Xaml:

公共类参数网格
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
公共列表参数{get;set;}
public SamResult SelectedParamJson{get;set;}
}

您的祖先不是数据网格,而是要从中获取数据上下文的数据行

因此,您创建了一些源集合(如列表)的集合视图,这意味着Datagrid中的每一行都代表一个IJsonObject对象,您应该在那里定义ComboBox列的集合。类似于可以绑定到列的列表,然后可以拥有可以绑定到选定项的IJsonProperty字段

因此,您应该得到一个组合框,其中填充有yout列表项,如果您选择其中一项,IJsonProerty字段将设置为所选项


我希望它可以理解,因为我现在没有真正的代码片段。

您的祖先不是datagrid,而是您想要从中获取datacontext的datarow

因此,您创建了一些源集合(如列表)的集合视图,这意味着Datagrid中的每一行都代表一个IJsonObject对象,您应该在那里定义ComboBox列的集合。类似于可以绑定到列的列表,然后可以拥有可以绑定到选定项的IJsonProperty字段

因此,您应该得到一个组合框,其中填充有yout列表项,如果您选择其中一项,IJsonProerty字段将设置为所选项


我希望这是可以理解的,因为我现在没有真正的代码片段。

如果要绑定到与
ParamsJson
属性定义在同一类中的
SelectedParamJson
属性,则不应将
RelativeSource
属性设置为任何内容:

ItemsSource="{Binding Path=ParamsJson}"
SelectedItem="{Binding Path=SelectedParamJson}"

如果要绑定到与
ParamsJson
属性在同一类中定义的
SelectedParamJson
属性,还应将
组合框
移动到
DataGridTemplateColumn
CellEditingTemplate
,您不应将
RelativeSource
属性设置为任何内容:

ItemsSource="{Binding Path=ParamsJson}"
SelectedItem="{Binding Path=SelectedParamJson}"

您还应该将
组合框
移动到
DataGridTemplateColumn
CellEditingTemplate
中。问题是您必须定义
,在那里您应该执行所有绑定和
,当单元格未激活时,该值将显示

这似乎很奇怪,您无法绑定
中的所有内容,因为您必须执行额外的klick来激活combobox

适用于我的Xaml:

    <DataGridTemplateColumn Header="Parameter" >

       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
             <TextBlock Text="{Binding Path=SelectedParamJson}" />
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>

       <DataGridTemplateColumn.CellEditingTemplate>
          <DataTemplate>
             <ComboBox MinWidth="100"
                       MaxWidth="150"
                       ItemsSource="{Binding Path=ParamsJson}"
                       SelectedItem="{Binding Path=SelectedParamJson}"
                       StaysOpenOnEdit="True"
                       IsEditable="True">
                <ComboBox.ItemTemplate>
                   <DataTemplate>
                      <TextBlock Text="{Binding}" />
                   </DataTemplate>
                </ComboBox.ItemTemplate>
             </ComboBox>
          </DataTemplate>
       </DataGridTemplateColumn.CellEditingTemplate>

    </DataGridTemplateColumn>

问题是您必须定义
,您应该在其中执行所有绑定和
,即当单元格未激活时的当前值

这似乎很奇怪,您无法绑定
中的所有内容,因为您必须执行额外的klick来激活combobox

适用于我的Xaml:

    <DataGridTemplateColumn Header="Parameter" >

       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
             <TextBlock Text="{Binding Path=SelectedParamJson}" />
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>

       <DataGridTemplateColumn.CellEditingTemplate>
          <DataTemplate>
             <ComboBox MinWidth="100"
                       MaxWidth="150"
                       ItemsSource="{Binding Path=ParamsJson}"
                       SelectedItem="{Binding Path=SelectedParamJson}"
                       StaysOpenOnEdit="True"
                       IsEditable="True">
                <ComboBox.ItemTemplate>
                   <DataTemplate>
                      <TextBlock Text="{Binding}" />
                   </DataTemplate>
                </ComboBox.ItemTemplate>
             </ComboBox>
          </DataTemplate>
       </DataGridTemplateColumn.CellEditingTemplate>

    </DataGridTemplateColumn>


不幸的是
SelectedItem=“{Binding Path=SelectedParamJson,RelativeSource={RelativeSource AncestorType=DataGridRow}}”
不适用于mewell,如果绑定到datagrid行的对象上没有名为SelectedParamJson的属性,它就无法工作。因此,您需要绑定到对象公开的对象,或者如果您获得了一个更全局的属性,那么您可能需要找到一个类似usercontrol或window的祖先。同样,relative source正在acestors datacontext中查找公共proerties,因此您需要确定SelectedParamJson的定义位置以及它在哪个控件中定义了vm。有一个DataGrid具有
ItemsSource=“{Binding PropertiesGrid}”
。我认为DataGridTemplateColumn中的Combobx应该直接看到SelectedParamJson,而不使用任何AncestorType…如果您的数据是ParameterGrid对象的集合,那么每个都是ParameterGrid,反过来可以将is公共属性公开为Datagrid单元格绑定(列定义)因此,如果绑定到DataGridRow的祖先,则应该有一个名为SelectedParamJson的公共属性,可以绑定到Combobox的SelectedItem属性。因此,您的ComboBox Itemsource是ParamsJson,您选择的项目是SelectedParamJson。如果您已经在上下文中,则不需要查找祖先,因为SelectedParamJson应该在相同的范围内。不幸的是
SelectedItem=“{Binding Path=SelectedParamJson,RelativeSource={RelativeSource AncestorType=DataGridRow}”
如果绑定到datagrid行的对象上没有名为SelectedParamJson的属性,则mewell无法使用该属性。因此,您需要绑定到对象公开的对象,或者如果您获得了一个更全局的属性,那么您可能需要找到一个类似usercontrol或wi的祖先
    <DataGridTemplateColumn Header="Parameter" >

       <DataGridTemplateColumn.CellTemplate>
          <DataTemplate>
             <TextBlock Text="{Binding Path=SelectedParamJson}" />
          </DataTemplate>
       </DataGridTemplateColumn.CellTemplate>

       <DataGridTemplateColumn.CellEditingTemplate>
          <DataTemplate>
             <ComboBox MinWidth="100"
                       MaxWidth="150"
                       ItemsSource="{Binding Path=ParamsJson}"
                       SelectedItem="{Binding Path=SelectedParamJson}"
                       StaysOpenOnEdit="True"
                       IsEditable="True">
                <ComboBox.ItemTemplate>
                   <DataTemplate>
                      <TextBlock Text="{Binding}" />
                   </DataTemplate>
                </ComboBox.ItemTemplate>
             </ComboBox>
          </DataTemplate>
       </DataGridTemplateColumn.CellEditingTemplate>

    </DataGridTemplateColumn>