从ListView获取GridViewColumn绑定路径名?

时间:2014-10-13 12:22:21

标签: c# wpf xaml listview gridviewcolumn

我有xaml代码

<ListView Name="List1" Margin="0,33,0,0"

                ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                ScrollViewer.VerticalScrollBarVisibility="Visible"
    ItemsSource="{Binding Path=Table}" MouseDoubleClick="List1_MouseDoubleClick">

        <ListView.View>

        <GridView x:Name="_gridView" ColumnHeaderTemplate="{StaticResource BlueHeader}">
            <GridViewColumn x:Name="LoyaltyCode"  Header="Code" DisplayMemberBinding="{Binding Path=LoyaltyCode}"/>
            <GridViewColumn x:Name="LoyaltyName" Header="Name" Width="130" DisplayMemberBinding="{Binding Path=LoyaltyName}" HeaderTemplate="{StaticResource BlueHeaderName}"/>
            <GridViewColumn Header="PurProductPonits" DisplayMemberBinding="{Binding Path=PurProductPonits}"/>
            <GridViewColumn Header="PurProductAmount" DisplayMemberBinding="{Binding Path=PurProductAmount}"/>
            <GridViewColumn x:Name="PurServicePonits" Header="PurServicePonits" DisplayMemberBinding="{Binding Path=PurServicePonits}"/>
            <GridViewColumn Header="PurServiceAmount" DisplayMemberBinding="{Binding Path=PurServiceAmount}"/>
            <GridViewColumn Header="RedeemPoints" DisplayMemberBinding="{Binding Path=RedeemPoints}"/>
            <GridViewColumn Header="RedeemAmount  " DisplayMemberBinding="{Binding Path=RedeemAmount}" />

            <GridViewColumn Header="LoyaltyID" DisplayMemberBinding="{Binding Path=LoyaltyID}" Width="0"/>

            <GridView.ColumnHeaderContainerStyle>
                <Style TargetType="{x:Type GridViewColumnHeader}">
                    <Setter Property="IsEnabled" Value="False"/>
                    <Style.Triggers>
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter Property="TextElement.Foreground" Value="Black"/>
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </GridView.ColumnHeaderContainerStyle>
        </GridView>
    </ListView.View>

</ListView>

背后的代码是

foreach (GridViewColumn column in _gridView.Columns)
{
    if (column.Width == 0)
        continue;

    //ComboBoxItem item = new ComboBoxItem();
    // item.Content=column.Header;
    // item.Tag=column.???;
    // cmbSearch.Items.Add(item);

    cmbSearch.Items.Add(column.Header);
}

我需要使用&#34; DisplayMemberBinding路径名称&#34;使用column.Header和值项填充组合框的显示项。 例如.. item.Content =代码, item.Tag = LoyaltyCode

1 个答案:

答案 0 :(得分:2)

这个答案迟了一年,但您可以做的是将每列的DisplayMemberBinding属性转换为Binding对象,这样您就可以访问Path属性并从那里获取属性名称。所以像这样:

    foreach (GridViewColumnHeader column in _gridView.Columns)
    {
        if (column.Width == 0)
            continue;
        ComboBoxItem item = new ComboBoxItem();
        item.Content=column.Header;
        string property = ((System.Windows.Data.Binding)column.DisplayMemberBinding).Path.Path;
        item.Tag= property;
        cmbSearch.Items.Add(item);

        cmbSearch.Items.Add(column.Header);
    }