将WPF组合框绑定到实体
好吧,我在互联网上搜索了很多,但似乎找不到正确的答案。 我有一个组合框,它绑定到EntityFramework实体的CollectionViewSource。 显示屏是一个显示姓名的模板。 问题是,当选择一个项目时,组合框的显示==object.ToString()(即MyCompany.Data.Professional),而不是有用的内容 我肯定我错过了这里的一处房产。ri是用户控件,SelectedMedicine是该控件的从属属性将WPF组合框绑定到实体,wpf,entity-framework,data-binding,combobox,Wpf,Entity Framework,Data Binding,Combobox,好吧,我在互联网上搜索了很多,但似乎找不到正确的答案。 我有一个组合框,它绑定到EntityFramework实体的CollectionViewSource。 显示屏是一个显示姓名的模板。 问题是,当选择一个项目时,组合框的显示==object.ToString()(即MyCompany.Data.Professional),而不是有用的内容 我肯定我错过了这里的一处房产。ri是用户控件,SelectedMedicine是该控件的从属属性 <ComboBox Grid.Column="1"
<ComboBox Grid.Column="1" Grid.Row="4" x:Name="cmbReferringPhys"
IsEditable="{Binding IsReadOnly}"
ItemsSource="{Binding Source={StaticResource ProfessionalLookup}}"
SelectedItem="{Binding ElementName=ri, Path=SelectedPhysian, Mode=TwoWay, UpdateSourceTrigger=LostFocus, NotifyOnValidationError=True, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}"
HorizontalAlignment="Left" VerticalAlignment="Top" Height="19.277" Width="300"
IsSynchronizedWithCurrentItem="True"
SelectionChanged="ReferringPhy_SelectionChanged" TabIndex="4">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100" />
<ColumnDefinition Width="120" />
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding FirstName}" Grid.Column="0" Margin="4,0" />
<TextBlock Text="{Binding LastName}" Grid.Column="1" Margin="4,0"/>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
您可以使用转换器(),但这可能有点过头了。我建议使用ComboBox的DisplayMemberPath属性
要使用转换器,您需要创建一个转换器类(实现IValueConverter的类)。您的代码如下所示:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
MyCompany.Data.Professional prof = (MyCompany.Data.Professional)value;
if (prof == null)
{
return string.Empty;
}
else
{
return string.Format("{0} {1}", prof.First, prof.Last); //or however you want to format this
}
}
// If you'll ever need to convert back from a string to a Professional,
// implement that logic here
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource [ConverterClassName]}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在XAML中,您将执行以下操作:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
MyCompany.Data.Professional prof = (MyCompany.Data.Professional)value;
if (prof == null)
{
return string.Empty;
}
else
{
return string.Format("{0} {1}", prof.First, prof.Last); //or however you want to format this
}
}
// If you'll ever need to convert back from a string to a Professional,
// implement that logic here
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource [ConverterClassName]}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
编辑:在这种情况下,我认为您永远不需要使用ConvertBack,因为您可以通过使用ComboBox.SelectedItem作为专业人员来获得所选对象。您可以使用converter(),但这可能有点过头了。我建议使用ComboBox的DisplayMemberPath属性
要使用转换器,您需要创建一个转换器类(实现IValueConverter的类)。您的代码如下所示:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
MyCompany.Data.Professional prof = (MyCompany.Data.Professional)value;
if (prof == null)
{
return string.Empty;
}
else
{
return string.Format("{0} {1}", prof.First, prof.Last); //or however you want to format this
}
}
// If you'll ever need to convert back from a string to a Professional,
// implement that logic here
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource [ConverterClassName]}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
在XAML中,您将执行以下操作:
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
MyCompany.Data.Professional prof = (MyCompany.Data.Professional)value;
if (prof == null)
{
return string.Empty;
}
else
{
return string.Format("{0} {1}", prof.First, prof.Last); //or however you want to format this
}
}
// If you'll ever need to convert back from a string to a Professional,
// implement that logic here
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return null;
}
<ComboBox>
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Converter={StaticResource [ConverterClassName]}}" />
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
编辑:在这种情况下,我认为您永远不需要使用ConvertBack,因为您可以通过将ComboBox.SelectedItem转换为专业人员来获取所选对象。例如,您需要使用DisplayMemberPath=“Data.Professional”。如果这对您有帮助,请告诉我。DisplayMemberPath的问题是FirstName和LastName是独立的字段。我需要的是类似于DisplayMemberPath模板的东西:)。那么,您将如何使用转换器(组合框上的哪个属性)?我在评论中没有足够的空间,但我更新了我的答案。希望这有帮助:)例如,您可能希望使用DisplayMemberPath=“Data.Professional”。如果这对您有帮助,请告诉我。DisplayMemberPath的问题是FirstName和LastName是独立的字段。我需要的是类似于DisplayMemberPath模板的东西:)。那么,您将如何使用转换器(组合框上的哪个属性)?我在评论中没有足够的空间,但我更新了我的答案。希望这有帮助:)