none
Change Different Control with Trigger? RRS feed

  • Question

  • Hello,
    is there a way to change the property of one control with a trigger of another?
    For examle, if a have a button and a groubbox, i want to set a trigger for the button, that when the button is hovered, the IsEnabled property of the groubbox is set to false. But i can't refer to that groubbox in the setter, is there another way to do that with a trigger though? of course i could easily do it in many other ways, disabling the groupbox, but with a trigger would be much better and less in code.
    thx
    Thursday, March 27, 2008 11:20 PM

Answers

  •  salat2 wrote:

    For examle, if a have a button and a groubbox, i want to set a trigger for the button, that when the button is hovered, the IsEnabled property of the groubbox is set to false.


    You can use DataTrigger inside Style triggers to implement this type of feature, here is an example:

    Code Snippet

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
            <Button Width="100" Height="30" Content="Click Me" x:Name="btn"/>
            <Button Width="100" Height="30" Content="Content">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </StackPanel>
    </Page>


    Hope this helps
    Monday, March 31, 2008 3:59 AM
  • Why can't you refer to the groupbox in the setter?  You should in theory be able to do something like:

     

    <Setter TargetName="GroupBoxControl" Property="IsEnabled" Value="False"/>

     

    Thursday, March 27, 2008 11:40 PM

All replies

  • Why can't you refer to the groupbox in the setter?  You should in theory be able to do something like:

     

    <Setter TargetName="GroupBoxControl" Property="IsEnabled" Value="False"/>

     

    Thursday, March 27, 2008 11:40 PM
  • thx, i just didn't come across that command yet^^

    Edit:
    But as i tried this method now as well, it doesn't work. Because the targetName property is only intended for controls in inside the control where the setter is used, which means i can't affect controls beyond the scope if the parent, like the button in this case. I tried it and get the error "TargetName property cannot be set on a Style Setter.".
    but if i'm wrong, and just didn't use it the right way, please tell me, just didn't work yet.
    Or if there's still another way to affect a control with setters of another control, please let me know.
    thx
    Thursday, March 27, 2008 11:52 PM
  • Can u post a small sample to demonstrate what you're doing now?

     

    Friday, March 28, 2008 3:01 PM
  • For now, i just added an eventHandler for SelectionChanged, like this:
    Code Snippet

    <ListView SelectionChanged="lv_SelectionChanged">
    ...



    and in code behind like that:
    Code Snippet

    private void lv_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                if (((ListView)sender).SelectedIndex == -1)
                    groupBox.IsEnabled = false;

    else groupBox.IsEnable = true;      

    }


    but although this works perfectly, i still want to do it with trigger. So if there's a way to affect a control with a setter of a different control, please let me know.
    Friday, March 28, 2008 3:57 PM
  • I would actually need to see your xaml - specifically the Style element that defines the items you're talking about.  You can affect any items that are contained within the same control template - if they are contained in completely different styles than you would not be able to access them via triggers.

     

    So, in your case, if your ListView and GroupBox were part of the same Control Template, you should be able to use the trigger method.
    Friday, March 28, 2008 5:42 PM
  • I know, and that's the problem, they are not part of the same control template. It looks like this:

    Code Snippet

    <StackPanel>

    <ListView>...</ListView>

    <GroupBox>...</GroupBox>

    </StackPanel>
    and the style to disable the groupBox "should" then look like this:

    Code Snippet

    <StackPanel>

    <ListView>
    <ListView.Style>
    <Style>
    <Trigger Property="SelectedIndex" Value="-1">
    <Setter TargetName="GB" Property="IsEnabled" Value="False"/>
    </Trigger>
    </Style>
    </ListView>

    <GroupBox Name="GB">...</GroupBox>

    </StackPanel>
    but as you said, i can't affect controls out of scope of the controlTemplate. But i hope there is another way to achieve this with triggers and setters. Otherwise, i would have to do with the event as mentioned before

    Friday, March 28, 2008 5:57 PM
  •  salat2 wrote:

    For examle, if a have a button and a groubbox, i want to set a trigger for the button, that when the button is hovered, the IsEnabled property of the groubbox is set to false.


    You can use DataTrigger inside Style triggers to implement this type of feature, here is an example:

    Code Snippet

    <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <StackPanel>
            <Button Width="100" Height="30" Content="Click Me" x:Name="btn"/>
            <Button Width="100" Height="30" Content="Content">
                <Button.Style>
                    <Style TargetType="{x:Type Button}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
            </Button>
        </StackPanel>
    </Page>


    Hope this helps
    Monday, March 31, 2008 3:59 AM
  • thanks
    that is exactly what i was searching for
    Tuesday, April 1, 2008 6:04 PM