这是我的第一个WPF应用程序。我目前有一个绑定到实体框架对象的DataGrid。该对象包含一个类作为其联系人项(名字,中间名等)之一。
所以我的问题是:如何使用该类的元素在数据列上使用MultiBinding?
这是代码。我正在尝试使用转换器来绑定联系人姓名。转换器根据传递的参数显示名称。
<DataGrid Grid.Row="2"
x:Name="GridResult"
AutoGenerateColumns="False"
CanUserAddRows="False"
MinHeight="{Binding DataGridMinHeight}"
ItemsSource="{Binding Results}"
BorderBrush="{StaticResource DarkBlueBrush}"
BorderThickness="0 1 0 0"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
IsReadOnly="True"
FontFamily="{StaticResource DefaultFont}"
HeadersVisibility="Column"
ColumnHeaderStyle="{StaticResource DataGridHeaderCenter}">
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick"
Command="{Binding DetailsCommand}"
CommandParameter="{Binding ElementName=GridResult, Path=SelectedItem.AssistanceId}" />
</DataGrid.InputBindings>
<DataGrid.Columns>
<DataGridTextColumn x:Name="NameColumn"
Binding="{Binding LinkName}"
Header="Reference"
Width="Auto" />
<DataGridTextColumn x:Name="CompanyColumn"
Binding="{Binding Company.CompanyName}"
Header="Company"
Width="Auto" />
<DataGridTextColumn x:Name="ContactColumn"
Header="Contact"
Width="Auto" >
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource NameConverter}" ConverterParameter="LastNameFirst" >
<Binding Source="Contact" Path="Salutation"/>
<Binding Source="Contact" Path="FirstName"/>
<Binding Source="Contact" Path="MiddleName"/>
<Binding Source="Contact" Path="LastName"/>
<Binding Source="Contact" Path="Suffix"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
答案 0 :(得分:0)
所以我知道了。不必尝试将Contact类的每个元素传递给MultiBinding,只需传递类本身即可。然后使用转换器访问类元素(例如,名字,姓氏等)以返回所需的输出字符串。
<DataGridTextColumn x:Name="ContactColumn"
Header="Contact"
Width="Auto"
>
<DataGridTextColumn.Binding>
<MultiBinding Converter="{StaticResource NameConverter}" ConverterParameter="LastNameFirst">
<Binding Path="Contact"/>
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>