是否可以将 WPF Combobox.SelectedValue 绑定到多个 ObjectDataProvider?
尝试确定是否可以使用 XAMAL 绑定将 ComboBox 的 SelectedValue 绑定到多个 ObjectDataProvider 的输入。
我查看了 MultiBinding,但这似乎将多个控件组合在一起,这并不完全是我今天想要的。
我希望能够让 ComboBox(位置)更改它所做的 TextBlock(偏差),并调用 ObjectDataProvider(CommentProvider)来更新 TextBox(locationComments)。
这在代码隐藏中相当简单,但我宁愿不走这条路作为学习经历。
XAMAL代码
<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" />
Trying to determine if it is possible to bind the SelectedValue of a ComboBox to the inputs of multiple ObjectDataProviders with XAMAL Bindings.
I looked at MultiBinding but that appears to be grouping multiple controls together, not exactly what I'm looking to day.
I'd like to be able to have the ComboBox (locations) change the TextBlock (deviance) which it does AND to call the ObjectDataProvider (CommentProvider) to update the TextBox (locationComments).
This is fairly straightforward in a code-behind but would prefer to not go this route as a learning experience.
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" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 MultiBinding 您走在正确的轨道上。
关键是将 MultiValueCoverter 与 MultiBinding 结合使用。
之前我们只绑定到一件事,现在我们将它绑定到两个 ObjectDataProvider。 让我们做到这一点的关键因素是转换器:
因为我们只需要在两个地方使用相同的值,所以 CovertBack 方法非常简单,但是我确信您可以看到它可以用于解析一些复杂的内容并传回不同的组件放置在 UI 中的不同位置。
使用此转换器,我们还可以尝试使用两个文本框来尝试一个小示例:
(我的示例中的 Converter 作为单独的类存在于 Window 的代码后面)
正如您所看到的,测试它会在 SelectedValue 更改时更新两个 TextBox。
You're on the right track with the MultiBinding.
The key is to use a MultiValueCoverter in conjunction with the MultiBinding.
Where we were binding to just one thing before, now we are binding it to both ObjectDataProviders. The key factor that lets us do this is the converter:
Because we just need the same value in both places the CovertBack method is quite simple, however I'm sure you can see that it could be used to parse some complex stuff and pass back different components to different places in the UI.
Using this converter we can also try out a small sample, using two text boxes instead:
(The Converter in my example exists in the Window's code behind as a separate class)
And as you can see testing this out it will update both TextBoxes when the SelectedValue changes.