我如何可以调用XAML与TabItem的名称的参数绑定方法? [英] How can I call a bound method in XAML with the parameter of a TabItem's name?
问题描述
我有在XAML绑定一个对象的实例。它有将回到我的其他对象的列表的方法(基于属性值我通过这个方法,它将返回所有对象与属性值)。
I've got an instance of an object bound in XAML. It's got a method that will return to me a list of other objects (based on the property value I pass this method, it will return all the objects with that property value).
<ObjectDataProvider ObjectInstance="_this.DataContext" MethodName="GetListByCategory" x:Key="List">
<ObjectDataProvider.MethodParameters>
<System:String>Windows</System:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
我(最终)希望与一些令人费解的DataTemplate一个选项卡上显示该列表。而不是获得超前了。
I (eventually) want to display this list on a tab with some convoluted DataTemplate. But not to get ahead of myself.
我希望得到所有应显示在一个特定的TabItem的一个TabControl(那些具有一个类别匹配TabItem的名称或标题内的对象 - 这是在完成的方法)。我如何通过我的绑定方法有关TabItem的的标题或名称?
I want to get all the objects that should be displayed on a particular TabItem inside a TabControl (ones that have a category matching the TabItem's name or header - this is done in the method). How do I pass my bound method the relevant TabItem's header or name?
请记住,我需要绑定到这个绑定方法的返回值在一个DataTemplate中显示在TabItem的。我不知道这是否必然相关的问题的答案,但我要确保我在界定清楚。
Bear in mind that I need to bind to the return value of this bound method to display in a DataTemplate in the TabItem. I don't know if that would necessarily be relevant to the answer of the problem but I want to make sure that I'm clear in defining it.
推荐答案
不幸的是, MethodParameters
的的ObjectDataProvider
不能直接约束。
Unfortunately the MethodParameters
of an ObjectDataProvider
can't be bound directly.
您可以使用双向
或 OneWayToSource
绑定。下面是一个使用 Directory.GetFiles
为你的 GetListByCategory
法的替代一个例子:
You can get around this by using a TwoWay
or OneWayToSource
binding. Here's an example that uses Directory.GetFiles
as a substitute for your GetListByCategory
method:
<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:io="clr-namespace:System.IO;assembly=mscorlib"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid.Resources>
<x:Array x:Key="directories" Type="{x:Type sys:String}">
<sys:String>C:\</sys:String>
<sys:String>C:\Windows\</sys:String>
<sys:String>C:\Windows\System32\</sys:String>
</x:Array>
<ObjectDataProvider x:Key="fileList" ObjectType="{x:Type io:Directory}" MethodName="GetFiles">
<ObjectDataProvider.MethodParameters>
<!-- Initial value, this will get wiped out by the Binding below. -->
<sys:String>C:\</sys:String>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</Grid.Resources>
<TabControl ItemsSource="{StaticResource directories}">
<TabControl.ItemContainerStyle>
<Style TargetType="{x:Type TabItem}">
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate DataType="{x:Type sys:String}">
<ListBox ItemsSource="{Binding Source={StaticResource fileList}}"/>
</DataTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.ItemContainerStyle>
<TabControl.SelectedItem>
<Binding Source="{StaticResource fileList}"
Path="MethodParameters[0]"
BindsDirectlyToSource="True"
Mode="OneWayToSource"/>
</TabControl.SelectedItem>
</TabControl>
</Grid>
的
TabControl.SelectedItem
绑定到 ObjectDataProvider.MethodParameters [0]
使用OneWayToSource,这样,当我们改变标签,方法的参数更改为新的目录。
The TabControl.SelectedItem
is bound to the ObjectDataProvider.MethodParameters[0]
using OneWayToSource, so that when we change tabs, the method parameter changes to the new directory.
这篇关于我如何可以调用XAML与TabItem的名称的参数绑定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!