显示datagrid中所选项目的枚举

时间:2013-07-19 21:09:33

标签: c# wpf

当用户从我的WPF页面上的列表中选择一个项目时,我试图在另一个DataGrid中显示该项目的枚举列表:

<DataGrid Grid.Row="1" AutoGenerateColumns="False" IsReadOnly="True" 
    HeadersVisibility="Column"
    ItemsSource="{Binding SelectedAdGroupWithRoles.PrestoRoles}"
    SelectedItem="{Binding SelectedPrestoRole}">                    
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding PrestoRole}" Header="Role" Width="*"/>
    </DataGrid.Columns>
</DataGrid>

PrestoRole是一个枚举。

我不确定如何设置最终绑定:

<DataGridTextColumn Binding="{Binding PrestoRole}" Header="Role" Width="*"/>

我尝试过一些东西,但PrestoRole名称不会显示。我怎样才能让它显示出来?

在一个完全独立的表单上,我在列表框中列出了PrestoRole枚举的每个成员:

<ListBox Grid.Row="0" ItemsSource="{Binding Roles}" SelectedItem="{Binding SelectedRole}" Height="auto">

视图模型上的Roles属性如下所示:

public List<PrestoRole> Roles

枚举成员在那里显示得很好。我无法让它在DataGrid中显示。

1 个答案:

答案 0 :(得分:2)

这应该有效:

<DataGridTextColumn Binding="{Binding}" Header="Role" Width="*"/>

定义列时,列的DataContext设置为每行的当前项,因此您需要做的就是绑定它。

相关问题