如何使用TabItem的名称参数在XAML中调用绑定方法? [英] How can I call a bound method in XAML with the parameter of a TabItem's name?
问题描述
< ObjectDataProvider ObjectInstance =_ this.DataContextMethodName =GetListByCategoryx:Key =List>
< ObjectDataProvider.MethodParameters>
<系统:String> Windows< / System:String>
< /ObjectDataProvider.MethodParameters>
< / ObjectDataProvider>
我(最终)希望在带有一些复杂的DataTemplate的选项卡上显示此列表。但不要超越自己。
我想获取应该在TabControl中的特定TabItem上显示的所有对象(具有与TabItem的名称或标题匹配的类别的对象 - 这是在方法)。如何传递我的绑定方法相关的TabItem的标题或名称?
请记住,我需要绑定到此绑定方法的返回值以显示在DataTemplate在TabItem中。我不知道这是否会与问题的答案相关,但我想确保我明确定义它。
不幸的是,$ code> ObjectDataProvider的 MethodParameters
不能直接绑定。
您可以通过使用 TwoWay
或 OneWayToSource
绑定。这是一个使用 Directory.GetFiles
替代您的 GetListByCategory
方法的示例:
< 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 =directoriesType ={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 =fileListObjectType ={x:Type io:Directory}MethodName =GetFiles>
< ObjectDataProvider.MethodParameters>
<! - 初始值,这将被下面的Binding消除。 - >
< 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,这样当我们更改选项卡时,方法参数更改为新目录。
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>
I (eventually) want to display this list on a tab with some convoluted DataTemplate. But not to get ahead of myself.
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?
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.
Unfortunately the MethodParameters
of an ObjectDataProvider
can't be bound directly.
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>
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.
这篇关于如何使用TabItem的名称参数在XAML中调用绑定方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!