我有一个wpf datagridview,在其中一个列中我试图绑定到一个自定义类。类代码在这里:
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>
然后在datagridview中创建一个DataGridTemplateColumn
<DataGridTemplateColumn Header="Max Rank">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox SelectedValue="{Binding charRank}"
ItemsSource="{Binding Source={StaticResource ValidRanks}}"
SelectedValuePath="RankIdent" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
(代码重复进行编辑,但为了使其更加精简,我正在剥离它。)
我尝试了各种绑定方法,但无论我做什么,我仍然有完全空白的问题,并且输出窗口没有吐出绑定错误。我觉得我忽略了一些明显的东西,但我不确定它是什么。
Debug 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>
答案 0 :(得分:0)
我无法告诉您代码中缺少的内容,因为这种情况有点复杂,我们也看不到所有代码。
我建议你调试你的绑定并找出自己缺少的东西。 有很多方法可以做到这一点,我建议您关注this guide。
最简单的方法是:
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase"
添加到您的Window代码中。<ComboBox SelectedValue="{Binding charRank}" ItemsSource="{Binding Source={StaticResource ValidRanks} diag:PresentationTraceSources.TraceLevel=High}" SelectedValuePath="RankIdent" />
现在,输出窗口中将显示有关绑定或错误绑定的相关数据。