idish
idish

Reputation: 3270

Access DataTemplate element via xaml

I want to access txtLang(textblock existing in the template) TextBlock from my MainWindow xaml via CommandParameter.

How can I do that? Here's the code:

Main Window:

<TreeView x:Name="TreeView" ItemTemplate="{DynamicResource TreeViewDataTemplate}" ItemsSource="{Binding PLanguageCollection}"/>
<Button CommandParameter="{Binding ElementName=TreeView,Path="SelectedItem...//What is the path?

Template:

<DataTemplate x:Key="TreeViewDataTemplate">
    <StackPanel Orientation="Horizontal">
        <Image Height="25" Width="25" VerticalAlignment="Center"
               Source="{Binding ImagePath}"/>
            <TextBlock Name="txtLang"  VerticalAlignment="Center" Text="{Binding Language}" />
    </StackPanel>
</DataTemplate>

EDIT:

MainWindow:

   <Button Content="Create Project">

              <Button.CommandParameter>
                    <MultiBinding Converter="{StaticResource MyConverter}">
                        <Binding Path="Text" ElementName="txtDesc"/>
                        <Binding Path="Text" ElementName="txtName"/>
                        <Binding Path="SelectedItem" ElementName="ListBox"/>
                        <Binding Path="SelectedItem.Language" ElementName="TreeView"/>
                    </MultiBinding>
                </Button.CommandParameter>
            </Button>

Converter:

   public class MyConverter : IMultiValueConverter
    {
        public object Convert(object[] values)
        {
            Tuple<string, string> tuple = new Tuple<string, string>(
                (string)values[0], (string)values[1]);
            return (object)tuple;
        }

        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {

           if(values[3] is string)
        {
            Service1 service1 = new Service1();
            service1.CreateProject2((string) values[0], (string) values[1], (string) values[2], (string) values[3]);
        } 
        return null;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

The thrown exception:

Unable to cast object of type 'MS.Internal.NamedObject' to type 'System.String'. 

I understand this happens because the DependencyProperty at the begining is "Unset" because it's being loaded at runtime by the dataTemplate. But how can I prevent it from happening?

Upvotes: 0

Views: 406

Answers (1)

Rachel
Rachel

Reputation: 132618

Use {Binding SelectedItem.Language}, or possibly {Binding SelectedItem.DataContext.Language} if your SelectedItem returns a TreeViewItem instead of your data object

The objects inside a DataTemplate doesn't actually exist until the template is used, so you can't reference like you would other objects. In addition, if the template is used multiple times, you'll have multiple TextBlocks named "txtLang", so your code wouldn't know which object to refer to.

I always think of DataTemplates as cookie cutters. You can't reference the cookie until you use the cookie cutter to make at least one cookie :)

Upvotes: 2

Related Questions