我在WPF网格中使用Combobox时陷入了混乱。
我试图实现一个简单的联系表单,允许用户从组合框中选择一个Salutation。
class Contact
{
..
public int SalutationID
{
get { return _salutationid;}
set { _salutationid = value; }
}
}
class Salutation
{
public int ID
{
get { return _id;}
}
public string Description
{
get { return _description; }
}
}
..
and in code
ObservableCollection<Contact> Contacts = GetContacts();
ObservableCollection<Salutation> Salutations = GetSalutations();
grid.ItemsSource = Contacts;
colSalutations.ItemsSource = Salutations;
相关的XAML是:
<DataGridComboBoxColumn x:Name="colSalutation" Header="Title" SelectedValueBinding="{Binding SalutationID}" SelectedValuePath="ID" DisplayMemberPath="Description" />
我只在Salutation列中获得了网格中最后一个条目的条目 - 但是这一行无效 - 它不应该存在(除此条目之外的整行是空白的)。当我单击编辑(在任何行上)时,会出现一个包含所有正确条目的组合框,但是当我选择一个项目时,它会消失并且字段条目为空白。
我看了很多例子,我似乎做得很好,但显然没有。
有人可以告诉我哪里出错了吗?你可能已经意识到,我是WPF的新手。
由于
我也试过这个:
<DataGridComboBoxColumn.ElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsDropDownOpen" Value="True" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Path=Description}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.ElementStyle>
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsDropDownOpen" Value="True" />
<Setter Property="ItemTemplate">
<Setter.Value>
<DataTemplate>
<TextBlock Text="{Binding Path=Description}"></TextBlock>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
但是这一点,虽然显示一个组合框(并且仍然没有任何其他行)以及正确的数据,但是不允许我离开当前行,我还没有找到原因!
希望澄清问题,下面是完整的XAML:
</Grid.Resources>
<DockPanel>
<DockPanel Name="ButtonPanel" DockPanel.Dock="Top" LastChildFill="false">
<Button DockPanel.Dock="Left" Content="Save" x:Name="btnSave" Click="btnSave_Click" Height="28"/>
<Button DockPanel.Dock="Right" Content="Cancel" x:Name="btnCancel" Click="btnCancel_Click" Height="28"/>
</DockPanel>
<ProgressBar DockPanel.Dock="Top" Name="pbLoading" Minimum="0" Maximum="1" Height="16" IsIndeterminate="True" Margin="0,0,0,16" />
<DataGrid DockPanel.Dock="Top" x:Name="dgContacts" AutoGenerateColumns="false" CellEditEnding="dgContacts_CellEditEnding" PreviewKeyDown="dgContacts_PreviewKeyDown" BeginningEdit="dgContacts_BeginningEdit" >
<DataGrid.Columns>
<mui:DataGridTextColumn x:Name="colFirstName" Header="First Name" Binding="{Binding fldForename}"/>
<mui:DataGridTextColumn x:Name="colLastName" Header="Last Name" Binding="{Binding fldSurname}" />
<mui:DataGridTextColumn x:Name="colEmailName" Header="Email" Binding="{Binding fldEmail}"/>
<mui:DataGridTextColumn x:Name="colPhoneNumber" Header="Telephone" Binding="{Binding fldPhoneNumber}" />
<mui:DataGridComboBoxColumn
x:Name="colSalutation" Header="Title"
SelectedItemBinding="{Binding SalutationID}" SelectedValuePath="ID"
DisplayMemberPath="Description">
</mui:DataGridComboBoxColumn>
<mui:DataGridTextColumn x:Name="colAddressLine1" Header="Address 1" Binding="{Binding colAddressLine1}" />
<mui:DataGridTextColumn x:Name="colAddressLine2" Header="Address 2" Binding="{Binding colAddressLine2}" />
<mui:DataGridTextColumn x:Name="colAddressLine3" Header="Address 3" Binding="{Binding colAddressLine3}" />
<mui:DataGridTextColumn x:Name="colCity" Header="City" Binding="{Binding fldCity}" />
<mui:DataGridTextColumn x:Name="colCounty" Header="County" Binding="{Binding fldCounty}" />
<mui:DataGridTextColumn x:Name="colPostCode" Header="Postcode" Binding="{Binding fldPostCode}" />
<mui:DataGridTextColumn x:Name="colCountry" Header="Country" Binding="{Binding fldCountry}" />
</DataGrid.Columns>
</DataGrid>
</DockPanel>
</Grid>
</UserControl>
答案 0 :(得分:0)
您的ID属性中的setter丢失了。