views:

1486

answers:

1

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.

+2  A: 

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"&gt;
    <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.

Robert Macnee