Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Parent Property From Children in WPF

Tags:

c#

wpf

xaml

I've created a status bar:

    <StatusBar>
        <StatusBarItem >
            <WrapPanel>
                <Image Source="/MyApp;component/Images/icon.png" />
                <TextBlock Name="_StatusbarUser" Text="Username"  />
            </WrapPanel>
        </StatusBarItem>
    </StatusBar>

How to create a trigger, when i set the textblock visibility to collapse, it will also triger parent status bar visibility.

I've try using style bellow, but didn't work

   <StatusBar Grid.Row="2" Name="_Statusbar">
        <StatusBar.Resources>
            <Style TargetType="{x:Type StatusBarItem}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource AncestorType={x:Type TextBlock}}, Path=Visibility}" Value="Collapsed">
                        <Setter Property="Visibility" Value="Collapsed"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StatusBar.Resources>
        <StatusBarItem>
            <WrapPanel>
                <Image  Source="/MyApp;component/Images/icon.png" />
                <TextBlock Name="_StatusbarUser" Text="Username" />
            </WrapPanel>
        </StatusBarItem>
    </StatusBar>

Help please, thx

like image 259
Agung Nugroho Avatar asked Oct 08 '22 11:10

Agung Nugroho


1 Answers

Better bind parent property to child property:

<StatusBar>
    <StatusBarItem Visibility="{Binding ElementName=_StatusbarUser, Path=Visibility}">
        <WrapPanel>
            <Image Source="/MyApp;component/Images/icon.png" />
            <TextBlock Name="_StatusbarUser" Text="Username" />
        </WrapPanel>
    </StatusBarItem>
</StatusBar>
like image 191
LPL Avatar answered Oct 12 '22 11:10

LPL