locked
Bind from datatemplate RRS feed

  • Question

  • Hi
    I've created a custom control, EditListControl. In this control is a ListBox. This listbox uses a DataTemplate to show data.
     
    Is there a way to from the DataTemplate access a propertey in EditlistControl. I would like to use it to set the control to either edit mode or list mode.

    //lasse
    Lasse
    Saturday, October 3, 2009 5:48 PM

Answers

  • Hi Lasse Lindstrom,

    Have you tried the code like below?
    <DataTemplate.Triggers>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:EditListControl}}, Path=EditControlListProperty}">

                    <Setter TargetName="listPanel" Property="Visibility" Value="Collapsed" />

                </DataTrigger>

    </DataTemplate.Triggers>

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
    Tuesday, October 6, 2009 12:23 PM

All replies

  • You can use TemplateBinding. Something like this:

    <EditListControl xmlns.... >
    <ListBox Property="{TemplateBinding EditControlListProperty}"/>
    </EditListControl>

    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    Saturday, October 3, 2009 6:07 PM
  • The solution Bigsby gives is correct. However, keep in mind that this uses the default binding mode (oneway in most cases). The TemplateBinding is simply a shortcut to {Binding RelativeSource={RelativeSource TemplatedParent}}.

    So, if you need twoway binding, simply use this:

    <ListBox Property="{Binding RelativeSource={RelativeSource TemplatedParent}, Mode=TwoWay, Path=EditControlListProperty}" />



    Geert van Horrik - CatenaLogic
    Visit my blog: http://blog.catenalogic.com

    Looking for a way to deploy your updates to all your clients? Try Updater!
    Sunday, October 4, 2009 9:25 AM
  • Rigth you are, Geert.

    There is, in fact, another option.


    <ListBox Property="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:EditListControl}}, Path=EditControlListProperty}" />

    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    Sunday, October 4, 2009 2:12 PM
  • Hi and thanks.
    I am not sure if I understand your solutions or if I expressed my question in a not so clear way.

    My XAML looks something like this:
         <DataTemplate x:Key="MyDataTemplate">
            <StackPanel>
            <StackPanel x:Name="listPanel" Width="Auto" Orientation="Horizontal">
                        </StackPanel>
            <StackPanel x:Name="editPanel" Width="Auto" Orientation="Horizontal">
                        </StackPanel>
            </StackPanel>
            <DataTemplate.Triggers>
                <DataTrigger Binding="????????????????">
                    <Setter TargetName="listPanel" Property="Visibility" Value="Collapsed" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    
    
       <Style x:Key="EditListControlStyle" TargetType="{x:Type EditList:EditListControl }">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type EditList:EditListControl}">
                             <ListView  Style="{StaticResource  MyStyle}"  ItemsSource="{TemplateBinding ItemsSource}" ItemTemplate="{StaticResource MyDataTemplateTemplate}" />
                               ......
                               ......
    
    

    From MyDataTemplate I am trying to access a property in EditListControl, at the moment expressed as ?????????????? :)


    Lasse
    Sunday, October 4, 2009 5:13 PM
  • DataTriggers are Triggers that read properties on DataContext object. So, unless DataContext is you EditList:EditListControl, the property won't be reached.

    If you want to to access EditListControl you'll have to use Binding/RelativeSource/AncestorType I exposed. Not in a DataTrigger, of course.

    I this isn't clear enough please say what behavior you want to implement, i.e., what do you want to happen on the ListView DataTemplate according to EditListControl property.


    Bigsby, Lisboa, Portugal
    O que for, quando for, é que será o que é...
    Wenn ist das Nunstruck git und Slotermeyer? Ja! ... Beiherhund das Oder die Flipperwaldt gersput!
    http://bigsby.eu
    Sunday, October 4, 2009 6:33 PM
  • Hi
    The idea was to change between list and edit mode. Example.. list mode contains TextBlock  and in edit mode those are changed to TextBox,,, Basically I've would like the DataTemplate swith between two views,,, one with not editable elements and one with editable elements.

    I've tried to reach properties in DataContext but not succed..


    //lasse
    Lasse
    Sunday, October 4, 2009 7:49 PM
  • Hi Lasse Lindstrom,

    Have you tried the code like below?
    <DataTemplate.Triggers>

                <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:EditListControl}}, Path=EditControlListProperty}">

                    <Setter TargetName="listPanel" Property="Visibility" Value="Collapsed" />

                </DataTrigger>

    </DataTemplate.Triggers>

    Thanks.
    Sincerely.


    Jim Zhou -MSFT
    Tuesday, October 6, 2009 12:23 PM
  • Hi Jim
    It seems to work (correcting your syntax).

    One drawback is that this DataTemplate is now connected to a EditListControl. My intention was to create a DataTemplate that could be used every time a specific class was used. What I mean,, I have a business model class called PersonModel. Every time I would like to show a PersonModel class in my UI,, I use this DataTemplate. This gives a uniform way to present PersonModel.

    //lasse
    Lasse
    Tuesday, October 6, 2009 8:17 PM
  • Hi Lasse Lindstrom,

    -->One drawback is that this DataTemplate is now connected to a EditListControl.
    Since you need to bind the property of EditListControl, so in the DataTemplate inside its control template, you need to use the ancestor searching binidng syntax discussed above.

    Thanks.
    Sincerely.
    Jim Zhou -MSFT
    Thursday, October 8, 2009 8:25 AM