简体   繁体   中英

Setting property in control from child in wpf

For the sake of simplicity say i have this control called DetailArea

<UserControl x:Class="DetailArea">
    <Grid>
       <CustomDataControl x:Name="MyDataControl" />
    </Grid>
</UserControl>

In another control i use my DetailArea

<UserControl x:Class="Display">
    <Grid>
        <DetailArea />
    </Grid>
</UserControl>

CustomDataControl in the DetailArea control has a public Visibility dependency property like all UI elements property that i want to be able to set to Hidden.

How can i do this from the Display control?

Edit: I cannot modify the DetailArea wpf or code as it's in another all which I don't control.

According to your Answers in the Comments, i suggest you to apply a Style which only affects the mentioned Control. Something like this

<UserControl x:Class="Display">
    <Grid>
        <Grid.Resources>
            <Style TargetType="{x:Type CustomDataControl}" BasedOn="{StaticResource KeyOfCustomDataControlStyle}">
                <Setter Property="Visibility" Value="Hidden"/>
            </Style>
        </Grid.Resources>
        <DetailArea />
    </Grid>
</UserControl>

Note: This is just a Pseudo Code, please modifiy it to match your Environment.

Note2: The BasedOn="{StaticResource KeyOfCustomDataControlStyle}" is optional.


Edit I've removed the x:Key="MakeInvisible" because the style may be not be applied correctly, details see here (Credits to @Jack).

I cannot modify the DetailArea wpf or code as it's in another all which I don't control.

Then you can't set a property of the CustomDataControl using XAML.

What you should do is to add add dependency property to the DetailArea class and bind the Visibility property of the CustomDataControl to this one. You can then set this property in the Display UserControl :

<DetailArea Visibility="Collapsed" />

If you can't modify DetailsArea for some reason, you will have to set the property programmatically, eg:

detailArea.MyDataControl.Visibility = Visibility.Collapsed;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM