通过wpf获取绑定项以显示在datagridview中的问题

本文关键字:datagridview 问题 显示 wpf 获取 绑定 通过 | 更新日期: 2023-09-27 18:25:22

我有一个wpf数据网格视图,在其中一列中,我试图绑定到一个自定义类。类代码在这里:

public class StarfleetRanks 
{
    SBRankType chainOfCommand;
    string DisplayValue;
    string RankIdent;
    int commandPriority;
    ....
    public override string ToString()
    {
        return this.DisplayValue;
    }
}

表单绑定到一个视图模型,在视图模型中,我在这里声明集合:

public class HomonculousViewModel : PropertyChangedBase
{
    public const string setVersion = "20151202-EXP";
    public static ObservableCollection<StarfleetRanks> ValidRanks { get; set;}

到目前为止还不错。在主xaml中,我将ValidRanks声明为静态资源,例如:

<Window.Resources>
    <CollectionViewSource x:Key="ValidRanks" Source="{Binding ValidRanks}" />
</Window.Resources>

然后在数据网格视图中,我创建了一个DataGridTemplateColumn

<DataGridTemplateColumn Header="Max Rank">
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
            <ComboBox   SelectedValue="{Binding charRank}"
                        ItemsSource="{Binding Source={StaticResource ValidRanks}}"
                        SelectedValuePath="RankIdent" />
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>

(还有为了编辑而复制的代码,但为了使其更加精简,我将其剥离。)

我已经尝试了各种绑定方法,但无论我做什么,它都是完全空白的,并且Output窗口不会显示绑定错误。我有一种感觉,我忽略了一些显而易见的东西,但我不确定它是什么

调试输出如下(将TraceLevel High附加到集合视图源和itemsSource的行):

System.Windows.Data Warning: 56 : Created BindingExpression 
    (hash=34610869) for Binding (hash=16019739)
System.Windows.Data Warning: 58 :   Path: 'progRanks'
System.Windows.Data Warning: 60 : BindingExpression (hash=34610869): 
    Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=34610869): 
    Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=34610869): Attach
    to System.Windows.Data.CollectionViewSource.Source (hash=25825509)
System.Windows.Data Warning: 64 : BindingExpression (hash=34610869): Use 
    Framework mentor <null>
System.Windows.Data Warning: 67 : BindingExpression (hash=34610869): 
    Resolving source 
System.Windows.Data Warning: 69 : BindingExpression (hash=34610869): 
    Framework mentor not found
System.Windows.Data Warning: 65 : BindingExpression (hash=34610869): 
    Resolve source deferred
System.Windows.Data Warning: 95 : BindingExpression (hash=34610869): Got 
    InheritanceContextChanged event from CollectionViewSource (hash=25825509)
System.Windows.Data Warning: 67 : BindingExpression (hash=34610869): 
    Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=34610869): 
    Found data context element: MainWindow (hash=10988211) (OK)
System.Windows.Data Warning: 78 : BindingExpression (hash=34610869): 
    Activate with root item HomonculousViewModel (hash=16930235)
System.Windows.Data Warning: 108 : BindingExpression (hash=34610869):   
    At level 0 - for HomonculousViewModel.progRanks found accessor 
        RuntimePropertyInfo(progRanks)
System.Windows.Data Warning: 104 : BindingExpression (hash=34610869): 
    Replace item at level 0 with HomonculousViewModel (hash=16930235), 
    using accessor RuntimePropertyInfo(progRanks)
System.Windows.Data Warning: 101 : BindingExpression (hash=34610869): 
    GetValue at level 0 from HomonculousViewModel (hash=16930235) using 
    RuntimePropertyInfo(progRanks): <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=34610869): 
    TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=34610869): 
    TransferValue - using final value <null>
System.Windows.Data Warning: 56 : Created BindingExpression (hash=61567818) 
    for Binding (hash=2511739)
System.Windows.Data Warning: 58 :   Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=61567818): 
    Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=61567818): 
    Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=63416782) 
    for Binding (hash=2511739)
System.Windows.Data Warning: 58 :   Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=63416782): 
    Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=63416782): 
    Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=63416782): 
    Attach to System.Windows.Controls.ComboBox.ItemsSource (hash=67884)
System.Windows.Data Warning: 67 : BindingExpression (hash=63416782): 
    Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=63416782): 
    Found data context element: <null> (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=63416782): 
    Use View from CollectionViewSource (hash=25825509)
System.Windows.Data Warning: 78 : BindingExpression (hash=63416782): 
    Activate with root item <null>
System.Windows.Data Warning: 104 : BindingExpression (hash=63416782): 
    Replace item at level 0 with <null>, using accessor 
    {DependencyProperty.UnsetValue}
System.Windows.Data Warning: 101 : BindingExpression (hash=63416782): 
    GetValue at level 0 from <null> using <null>: <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=63416782): 
    TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=63416782): 
    TransferValue - using final value <null>
System.Windows.Data Warning: 56 : Created BindingExpression (hash=42070525) 
    for Binding (hash=50108374)
System.Windows.Data Warning: 58 :   Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=42070525): 
    Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=42070525): 
    Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=13105554) 
    for Binding (hash=50108374)
System.Windows.Data Warning: 58 :   Path: ''
System.Windows.Data Warning: 60 : BindingExpression (hash=13105554): 
    Default mode resolved to OneWay
System.Windows.Data Warning: 61 : BindingExpression (hash=13105554): 
    Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=13105554): 
    Attach to System.Windows.Controls.ComboBox.ItemsSource (hash=50576745)
System.Windows.Data Warning: 67 : BindingExpression (hash=13105554): 
    Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=13105554): 
    Found data context element: <null> (OK)
System.Windows.Data Warning: 76 : BindingExpression (hash=13105554): 
    Use View from CollectionViewSource (hash=25825509)
System.Windows.Data Warning: 78 : BindingExpression (hash=13105554): 
    Activate with root item <null>
System.Windows.Data Warning: 104 : BindingExpression (hash=13105554): 
    Replace item at level 0 with <null>, using accessor {DependencyProperty.
    UnsetValue}
System.Windows.Data Warning: 101 : BindingExpression (hash=13105554): 
    GetValue at level 0 from <null> using <null>: <null>
System.Windows.Data Warning: 80 : BindingExpression (hash=13105554): 
    TransferValue - got raw value <null>
System.Windows.Data Warning: 89 : BindingExpression (hash=13105554): 
    TransferValue - using final value <null>

编辑:根据请求,这里是完整的XAML。注释掉的代码是另一种尝试,目的是让ItemsSource正确绑定

<Window x:Class="Homonculous.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:Homonculous"
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
    xmlns:SB118="clr-namespace:SB118_CrewHistoryApp"
    xmlns:SB118E="clr-namespace:SB118_CrewHistoryApp.Enums"
    mc:Ignorable="d"
    x:Name="prgWindow"
    Title="SB118 Crew History" Height="714" Width="936">
<Window.Resources>
    <local:DateTimeToStardateConverter x:Key="DTSconverter" />
    <local:YearToBrushConverter x:Key="YTBconverter" />
    <CollectionViewSource x:Key="validRanks" Source="{Binding progRanks, diag:PresentationTraceSources.TraceLevel=High}" />
    <local:BindingProxy x:Key="BTProxy" Data="{Binding }"/>
</Window.Resources>
<Grid>
    <DockPanel Margin="0,0,0,0"  LastChildFill="True">
        <Menu x:Name="menu" Height="20" DockPanel.Dock="Top" IsMainMenu="True">
            <MenuItem Header="_File" />
            <MenuItem Header="_Help">
                <MenuItem Header="_About" Click="DisplayAbout" />
            </MenuItem>
        </Menu>
        <TabControl x:Name="tabControl" HorizontalAlignment="Stretch">
            <TabItem Header="History Tracker">
                <Grid Background="#FFE5E5E5">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="200" />
                        <RowDefinition Height="34" />
                        <RowDefinition Height="17" />
                        <RowDefinition />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="82" />
                        <ColumnDefinition />
                        <ColumnDefinition Width="63" />
                        <ColumnDefinition Width="120" />
                    </Grid.ColumnDefinitions>
                    <Label Grid.Row="0" Grid.Column="0" x:Name="label" Content="Intro Text" />
                    <TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="3" x:Name="txtHeader" />
                    <Label Grid.Row="1" Grid.Column="0" x:Name="label1" Content="Ship Name" />
                    <TextBox Grid.Row="1" Grid.Column="1" x:Name="txtShipName" Margin="0,0,481,0" />
                    <Label Grid.Row="1" Grid.Column="2"  x:Name="label2" Content="Pip Style" />
                    <ComboBox Grid.Row="1" Grid.Column="3"  x:Name="cmbPipStyle">
                        <ComboBoxItem IsSelected="True">RetroSlanted</ComboBoxItem>
                        <ComboBoxItem>Slanted</ComboBoxItem>
                        <ComboBoxItem>STO</ComboBoxItem>
                        <ComboBoxItem>DS9</ComboBoxItem>
                        <ComboBoxItem>TOS</ComboBoxItem>
                        <ComboBoxItem>FI</ComboBoxItem>
                        <ComboBoxItem>FC</ComboBoxItem>
                    </ComboBox>
                    <DataGrid x:Name="dataGrid" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="4" AutoGenerateColumns="False"
                              ItemsSource="{Binding historyListing}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="First Name" Binding="{Binding charFirstN}"/>
                            <DataGridTextColumn Header="Last Name" Binding="{Binding charLastN}"/>                              
                            <DataGridTemplateColumn Header="Max Rank">
                                <!--
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <ComboBox ItemsSource="{Binding Data.progRanks, Source={StaticResource BTProxy}, diag:PresentationTraceSources.TraceLevel=High}" 
                                                  SelectedValuePath="{Binding charRank, Mode=OneWay}"
                                                  DisplayMemberPath="{Binding }"/>                                            
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding charRank}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                -->
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                            <ComboBox   SelectedValue="{Binding charRank}"
                                                        ItemsSource="{Binding Source={StaticResource ValidRanks}}"
                                                        SelectedValuePath="RankIdent" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                                <!-- <DataGridTemplateColumn Header="Max Rank"> -->
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <ComboBox   SelectedValue="{Binding charRank}"
                                                        ItemsSource="{Binding Source={StaticResource ValidRanks}}"
                                                        SelectedValuePath="RankIdent" />
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Join Date">
                                <DataGridTemplateColumn.CellStyle>
                                    <Style TargetType="{x:Type ContentControl}">
                                        <Setter Property="Background" Value="{Binding charJoinDate, Converter={StaticResource YTBconverter}}" />
                                    </Style>
                                </DataGridTemplateColumn.CellStyle>
                                <DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <ContentControl Content="{Binding charJoinDate}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellTemplate>
                                <DataGridTemplateColumn.CellEditingTemplate>
                                    <DataTemplate>
                                        <DatePicker IsTodayHighlighted="True" SelectedDate="{Binding Path=charJoinDate, Converter={StaticResource DTSconverter}, Mode=TwoWay}" />
                                    </DataTemplate>
                                </DataGridTemplateColumn.CellEditingTemplate>
                            </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Leave Date">
                            <DataGridTemplateColumn.CellStyle>
                                <Style TargetType="{x:Type ContentControl}">
                                    <Setter Property="Background" Value="{Binding charLeaveDate, Converter={StaticResource YTBconverter}}" />
                                </Style>
                            </DataGridTemplateColumn.CellStyle>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <ContentControl Content="{Binding charLeaveDate}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                            <DataGridTemplateColumn.CellEditingTemplate>
                                <DataTemplate>
                                    <DatePicker IsTodayHighlighted="True" SelectedDate="{Binding Path=charLeaveDate, Converter={StaticResource DTSconverter}, Mode=TwoWay}" />
                                </DataTemplate>
                            </DataGridTemplateColumn.CellEditingTemplate>
                        </DataGridTemplateColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </Grid>
            </TabItem>
            <TabItem Header="Positions and Promotions" Visibility="Hidden">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
            <TabItem Header="Awards" Visibility="Hidden">
                <Grid Background="#FFE5E5E5"/>
            </TabItem>
        </TabControl>
    </DockPanel>
    </Grid>
</Window>

通过wpf获取绑定项以显示在datagridview中的问题

我不能告诉你代码中缺少了什么,因为这是一个有点复杂的情况,我们没有看到所有的代码。

我建议您调试绑定,自己找出缺少的内容。有很多方法可以做到这一点,我建议你遵循本指南。

最简单的方法是:

  • 将此:xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"添加到您的Window标记中
  • 按照如下方式更改组合框:
<ComboBox   SelectedValue="{Binding charRank}"
                    ItemsSource="{Binding Source={StaticResource ValidRanks} diag:PresentationTraceSources.TraceLevel=High}"
                    SelectedValuePath="RankIdent" />

现在,有关绑定或未命中绑定的相关数据将显示在输出窗口中。