简体   繁体   中英

usercontrol inside an other user control. How to use properties?

I made a usercontrol with several element inside (toggles, button, text box, etc...). I'm using this usercontrol inside an other one, and load this final usercontrol at runtime and on my final application. I'm a little lost with dependency property, I would like to access the properties of the very first from my application.

The first usercontrol is called "Ch_Parameters" :

<UserControl
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:CustomControlTest"
         xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="CustomControlTest.ChannelParameters"
         mc:Ignorable="d" 
         x:Name="Ch_Parameters"
         d:DesignHeight="247.465" d:DesignWidth="436.624">

I'm trying to first communicate with a toggle button contained in "Ch_Parameters" and its properties "IsChecked"

<ToggleButton x:Name="Flip_X" Content="FLIP X" Style="{DynamicResource BaseToggleButtonStyle}" IsChecked="{Binding Path=DataContext.Flip_X, ElementName=Ch_Parameters, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

and here is a the code for the property

        public static readonly DependencyProperty FlipXProperty =
    DependencyProperty.Register("FlipX", typeof(bool), typeof(ChannelParameters), new PropertyMetadata(true));

    [Bindable(true)]
    public bool FlipX
    {
        get { return (bool)this.GetValue(FlipXProperty); }
        set { this.SetValue(FlipXProperty, value); }
    }

Now this usercontrol is used in other one called cmix :

         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:CustomControlTest"
         x:Name="cmix"
         xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="CustomControlTest.C_MiX_UserInterface"
         mc:Ignorable="d" d:DesignWidth="674.669" d:DesignHeight="337.953">

here :

<local:ChannelParameters FlipX="{Binding Path=Datacontext.VidFlipX, ElementName=cmix, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

and the code for the cmix property :

        public static readonly DependencyProperty VidFlipXProperty =
    DependencyProperty.Register("VidFlipX", typeof(bool), typeof(C_MiX_UserInterface), new PropertyMetadata(true));

    [Bindable(true)]
    public bool VidFlipX
    {
        get { return (bool)this.GetValue(VidFlipXProperty); }
        set { this.SetValue(VidFlipXProperty, value); }
    }

but, when I use the usercontrol "cmix" and load it at runtime, the property FLIPX from the Ch_Parameters usercontrol is not outputing anything when I trigger the toggle button.

var uiElement = (C_MiX_UserInterface)UIElementOut[i];
var toggle = uiElement.VidFlipX;

Is there a specific method to use properties from usercontrol inside an other usercontrol ?

Here is the solution that worked for me, as I in fact had the exact same problem :

Binding DependencyProperty in nested user controls

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