尝试确定是否可以将ComboBox的SelectedValue绑定到具有XAMAL绑定的多个ObjectDataProviders的输入。
我查看了MultiBinding,但这似乎是将多个控件组合在一起,而不是我正在寻找的那一天。
我希望能够让ComboBox(位置)改变它所做的TextBlock(偏差)并调用ObjectDataProvider(CommentProvider)来更新TextBox(locationComments)。
这在代码隐藏方面相当简单,但我宁愿不把这条路作为学习经验。
XAMAL CODE
<Window.Resources>
<ObjectDataProvider x:Key="LocationProvider"
ObjectType="{x:Type srv:ServiceClient}"
IsAsynchronous="True"MethodName="GetAssignedLocations" />
<ObjectDataProvider
x:Key="DevianceProvider"
ObjectType="{x:Type srv:ServiceClient}"
IsAsynchronous="True" MethodName="GetPercentChange">
<ObjectDataProvider.MethodParameters>
<system:String>Location1</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<ObjectDataProvider
x:Key="CommentProvider"
ObjectType="{x:Type srv:ServiceClient}"
IsAsynchronous="True"
MethodName="GetCommentByBusinessUnit">
<ObjectDataProvider.MethodParameters>
<system:String>Location1</system:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Window.Resources>
<ComboBox Height="23" HorizontalAlignment="Left" Margin="12,12,0,0" Name="locations" VerticalAlignment="Top" ItemsSource="{Binding Source={StaticResource LocationProvider}}"
DisplayMemberPath="BuName" SelectedValuePath="BuKey"
SelectionChanged="locations_SelectionChanged">
<ComboBox.SelectedValue>
<Binding Source="{StaticResource DevianceProvider}"
Path="MethodParameters[0]"
BindsDirectlyToSource="True"
Mode="OneWayToSource" />
</ComboBox.SelectedValue>
<TextBlock Name="deviance" Height="23" Margin="0,0,645,17" Width="40" Text="{Binding Source={StaticResource DevianceProvider}}" IsEnabled="False" />
<TextBox Height="23" Margin="0,0,181,17" Name="locationComments" Width="350" />
答案 0 :(得分:3)
您使用MultiBinding处于正确的轨道上。 关键是将MultiValueCoverter与MultiBinding一起使用。
<MultiBinding Converter="{StaticResource Coverter_LocationMultiConverter}"
Mode="OneWayToSource">
<Binding Source="{StaticResource DevianceProvider}"
Path="MethodParameters[0]"
BindsDirectlyToSource="True"
Mode="OneWayToSource" />
<Binding Source="{StaticResource CommentProvider}"
Path="MethodParameters[0]"
BindsDirectlyToSource="True"
Mode="OneWayToSource" />
</MultiBinding>
我们之前只绑定了一件事,现在我们将它绑定到两个ObjectDataProviders。让我们这样做的关键因素是转换器:
public class LocationMultiCoverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
return new object[] { value, value };
}
#endregion
}
因为我们在两个地方只需要相同的值,所以CovertBack方法非常简单,但是我确信你可以看到它可以用来解析一些复杂的东西并将不同的组件传回UI中的不同位置。
使用此转换器,我们也可以使用两个文本框来尝试一个小样本:
<Window x:Class="Sample.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Sample"
Title="Window1"
Height="300"
Width="300">
<Window.Resources>
<local:LocationMultiCoverter x:Key="Coverter_LocationMultiConverter" />
</Window.Resources>
<Grid>
<StackPanel>
<TextBlock x:Name="uiDeviance" />
<TextBlock x:Name="uiComment" />
<ComboBox x:Name="uiLocations"
Height="23"
HorizontalAlignment="Left"
VerticalAlignment="Top"
SelectedValuePath="Content">
<ComboBoxItem>1</ComboBoxItem>
<ComboBoxItem>2</ComboBoxItem>
<ComboBoxItem>3</ComboBoxItem>
<ComboBoxItem>4</ComboBoxItem>
<ComboBoxItem>5</ComboBoxItem>
<ComboBox.SelectedValue>
<MultiBinding Converter="{StaticResource Coverter_LocationMultiConverter}"
Mode="OneWayToSource">
<Binding ElementName="uiDeviance"
Path="Text"
BindsDirectlyToSource="True" />
<Binding ElementName="uiComment"
Path="Text"
BindsDirectlyToSource="True" />
</MultiBinding>
</ComboBox.SelectedValue>
</ComboBox>
</StackPanel>
</Grid>
(我的示例中的转换器作为单独的类存在于Window的代码中) 正如您所看到的那样,当SelectedValue发生更改时,它将更新两个TextBox。