locked
User Control - ListBox Style Triggers RRS feed

  • Question

  • Hi

    I have a user control that takes a ListBox as the content. To provide a default styling for the ListBox being supplied i created the style inside my user control. The style needs to have a trigger that changes an attribute of my user control. How can i do this? Also how could i get a value of my user control from the style e.g. trigger if the listboxitem is selected and user control is enabled?

    Friday, October 2, 2009 6:50 AM

Answers

  • Hi Syslock,

    -->The style needs to have a trigger that changes an attribute of my user control
    Bigsby is right. Usually, we could utilize data binding to bind the property of child control to that of the parent control(UserControl), but not the attributes.

    -->trigger if the listboxitem is selected and user control is enabled?
    Do you want to bind the IsEnable property of UserControl to IsSelected property of  the item container(ListBoxItem)? If so, you can use data trigger to do it like below:

     <Style TargetType="ListBoxItem">

                    <!--other code-->

                    <Style.Triggers>

                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"

                                     Value="True">

                            <Setter Property="IsSelected" Value="True" />

                        </DataTrigger>

                    </Style.Triggers>

     </Style>


    Thanks.
    Sincerely.

    Jim Zhou -MSFT
    • Marked as answer by Syslock Wednesday, October 7, 2009 11:41 AM
    Tuesday, October 6, 2009 8:49 AM

All replies

  • "The style needs to have a trigger that changes an attribute of my user control."
    This implementation doesn't make WPF sense. You can always bind the UserControl property to ListBox property. The Style scope "does not know" of the UserControl existence.

    "Also how could i get a value of my user control from the style e.g. trigger if the listboxitem is selected and user control is enabled?"
    Something like this should be enough:
    <ListBoxItem Selected="{TemplateBinding Enabled}"/>

    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
    Tuesday, October 6, 2009 12:47 AM
  • Hi Syslock,

    -->The style needs to have a trigger that changes an attribute of my user control
    Bigsby is right. Usually, we could utilize data binding to bind the property of child control to that of the parent control(UserControl), but not the attributes.

    -->trigger if the listboxitem is selected and user control is enabled?
    Do you want to bind the IsEnable property of UserControl to IsSelected property of  the item container(ListBoxItem)? If so, you can use data trigger to do it like below:

     <Style TargetType="ListBoxItem">

                    <!--other code-->

                    <Style.Triggers>

                        <DataTrigger Binding="{Binding Path=IsEnabled, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"

                                     Value="True">

                            <Setter Property="IsSelected" Value="True" />

                        </DataTrigger>

                    </Style.Triggers>

     </Style>


    Thanks.
    Sincerely.

    Jim Zhou -MSFT
    • Marked as answer by Syslock Wednesday, October 7, 2009 11:41 AM
    Tuesday, October 6, 2009 8:49 AM