locked
Making a control Visible when mouse is over an adjacent control in a stack panel using Triggers RRS feed

  • Question

  • Hi,
      I have a stack panel on a Window with Buttons and TextBlocks.When the mouse is over one of these buttons i want to make another control in the Stack panel visible which is set as invisible initially, by using just triggers.How can i achieve this?
    Is it possible for me to access another element on the Window in the trigger of one element, if so how?
    Thanks in advance...
    Thursday, September 3, 2009 2:52 PM

Answers

  • Zest4Quest,
    First look at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2abb9c3e-c86e-4f7f-ae6e-ada7f9947888 to see why I have the trigger exactly opposite what you really want. You want to use the triggers on controls the default behavior.

    Here is using DataTriggers and ElementName. Pay attention to myButton.

    <TextBlock Text="Hello">
        <TextBlock.Resources>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=myButton}" Value="False">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Resources>
    </TextBlock>

    Hope this helps.
    noorbakhsh
    • Marked as answer by Zest4Quest Thursday, September 3, 2009 4:04 PM
    Thursday, September 3, 2009 3:38 PM
  • <Window x:Class="MSDNTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="400"
        Height="400" >
        <Grid Name="grid" >
            <Control>
                <Control.Template>
                    <ControlTemplate TargetType="{x:Type Control}">
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="btn1" Property="Button.IsMouseOver" Value="True">
                                <Setter TargetName="tb1" Property="TextBox.Visibility" Value="Visible"/>
                            </Trigger>
                            <Trigger SourceName="btn2" Property="Button.IsMouseOver" Value="True">
                                <Setter TargetName="tb2" Property="TextBox.Visibility" Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                        <StackPanel>
                            <Button Name="btn1" Content="Button 1"/>
                            <Button Name="btn2" Content="Button 2"/>
                            <TextBox Name="tb1" Visibility="Hidden" Text="TextBox 1"/>
                            <TextBox Name="tb2" Visibility="Hidden" Text="TextBox 2"/>
                        </StackPanel>
                    </ControlTemplate>
                </Control.Template>
            </Control>
        </Grid>
    </Window>

    Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
    • Marked as answer by Zest4Quest Thursday, September 3, 2009 4:04 PM
    Thursday, September 3, 2009 3:44 PM

All replies

  • Zest4Quest,
    First look at http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/2abb9c3e-c86e-4f7f-ae6e-ada7f9947888 to see why I have the trigger exactly opposite what you really want. You want to use the triggers on controls the default behavior.

    Here is using DataTriggers and ElementName. Pay attention to myButton.

    <TextBlock Text="Hello">
        <TextBlock.Resources>
            <Style TargetType="TextBlock">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Path=IsMouseOver, ElementName=myButton}" Value="False">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Resources>
    </TextBlock>

    Hope this helps.
    noorbakhsh
    • Marked as answer by Zest4Quest Thursday, September 3, 2009 4:04 PM
    Thursday, September 3, 2009 3:38 PM
  • <Window x:Class="MSDNTest.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Width="400"
        Height="400" >
        <Grid Name="grid" >
            <Control>
                <Control.Template>
                    <ControlTemplate TargetType="{x:Type Control}">
                        <ControlTemplate.Triggers>
                            <Trigger SourceName="btn1" Property="Button.IsMouseOver" Value="True">
                                <Setter TargetName="tb1" Property="TextBox.Visibility" Value="Visible"/>
                            </Trigger>
                            <Trigger SourceName="btn2" Property="Button.IsMouseOver" Value="True">
                                <Setter TargetName="tb2" Property="TextBox.Visibility" Value="Visible"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                        <StackPanel>
                            <Button Name="btn1" Content="Button 1"/>
                            <Button Name="btn2" Content="Button 2"/>
                            <TextBox Name="tb1" Visibility="Hidden" Text="TextBox 1"/>
                            <TextBox Name="tb2" Visibility="Hidden" Text="TextBox 2"/>
                        </StackPanel>
                    </ControlTemplate>
                </Control.Template>
            </Control>
        </Grid>
    </Window>

    Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
    • Marked as answer by Zest4Quest Thursday, September 3, 2009 4:04 PM
    Thursday, September 3, 2009 3:44 PM