在数据网格内填充组合框

时间:2018-12-13 18:31:40

标签: python wpf datagrid

我正在尝试在WPF数据网格内填充一个组合框。 Xaml是这个

    <DataGrid x:Name="objects"  ItemsSource="{Binding}"  AutoGenerateColumns="False" HorizontalAlignment="Left" Height="91" Margin="96,56,0,0" VerticalAlignment="Top" Width="260" SelectionChanged="DataGrid_SelectionChanged" RenderTransformOrigin="0.581,0.499">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Object Name" Binding="{Binding objName}" ClipboardContentBinding="{x:Null}"/>
            <DataGridComboBoxColumn x:Name="dataGridComboBoxColumn"  Header="Material" ClipboardContentBinding="{x:Null}" ItemsSource="{Binding Items, RelativeSource={RelativeSource AncestorType=Window}}" SelectedValuePath="{Binding Path=SelectedAwaitingItem, Mode=TwoWay}" Width="Auto" TextBinding="{x:Null}"/>
        </DataGrid.Columns>

    </DataGrid>   

代码是

class objects(object):
  def __init__(self, objName,Items):
      self.objName = objName
      self.SelectedAwaitingItem = ""
      self.Items = Items    


    gridUnknownDocs = LogicalTreeHelper.FindLogicalNode(self, 'objects')
    item = []
    politetitles = {
    1 : ('Mr' ), 
    2 : ('Ms'),
    3 : ('Mrs'), 
    4 : ('Sir'),
    }


    item.append(objects("Cube",["3" "2"]))
    item.append(objects("Square",["1" "2"]))
    gridUnknownDocs.ItemsSource = item

我能够看到正确填充的文本框(带有Cube和Square值),但是组合框始终显示为空 关于我所缺少的任何建议吗? 谢谢 最高

2 个答案:

答案 0 :(得分:1)

任何以path结尾的属性都需要一个字符串,该字符串是属性的名称。 您具有对selectedvaluepath的绑定。 我不了解项目实际包含的内容,但是您需要告诉它哪个属性是值以及要显示哪个。 https://social.technet.microsoft.com/wiki/contents/articles/26347.wpf-combobox-binding-in-datagrid.aspx

答案 1 :(得分:0)

这是将DataGridComboBoxColumn的{​​{1}}属性绑定到视觉祖先的属性的方法:

<DataGridComboBoxColumn DisplayMemberPath="Material" ClipboardContentBinding="{x:Null}" SelectedValuePath="{Binding Path=SelectedAwaitingItem, Mode=TwoWay}" Width="Auto" TextBinding="{x:Null}">
    <DataGridComboBoxColumn.ElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" Value="{Binding Items, RelativeSource={RelativeSource AncestorType=Window}}" />
        </Style>
    </DataGridComboBoxColumn.ElementStyle>
    <DataGridComboBoxColumn.EditingElementStyle>
        <Style>
            <Setter Property="ComboBox.ItemsSource" Value="{Binding Items, RelativeSource={RelativeSource AncestorType=Window}}" />
        </Style>
    </DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>