Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Community,

I've created an usercontrol 'Splitter'.

xaml looks like this:

HTML
<UserControl x:Class="DesktopApp.UserControls.Splitter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Border x:Name="oBorder">
            
            <Border.Effect>
                <DropShadowEffect x:Name="oShadow" />
            </Border.Effect>
            
        </Border>
</UserControl>


I want to access some Properties of the oBorder and oShadow-Childs.

For example i want to set the Color-Property of the Child "oBorder" like this:

HTML
<uc:Splitter x:Name="oSplitter" SplitterColor="Orange"></uc:Splitter>


where uc is defined as following:

HTML
xmlns:uc="clr-namespace:DesktopApp.UserControls"


So I've put a DP called 'SplitterColor' into my usercontrol.

C#
public Brush SplitterColor
        {
            get { return (Brush)GetValue(SplitterColorProperty); }
            set { SetValue(SplitterColorProperty, value); }
        }


I've defined a default value 'Brushes.White'

C#
public static readonly DependencyProperty SplitterColorProperty =
            DependencyProperty.Register("SplitterColor", typeof(Brush), typeof(Splitter), new PropertyMetadata(Brushes.White, OnSplitterColorChanged));


This code changes the Color-Property of by oBorder Child. In this example dNewBackgroundColor will be an Orange brush.

C#
private static void OnSplitterColorChanged(DependencyObject sender, DependencyPropertyChangedEventArgs eventArgs)
        {
            Brush dNewBackgroundColor = eventArgs.NewValue as Brush;
            Splitter oThis;

            if (dNewBackgroundColor != null)
            {
                oThis = sender as Splitter;

                if (oThis != null)
                {
                    //Zuweisung
                    oThis.oBorder.Background = dNewBackgroundColor;
                }
            }
        }



It works fine !

But if i define my control as the following:

HTML
<uc:Splitter x:Name="oSplitter" SplitterColor="White"></uc:Splitter>


or

HTML
<uc:Splitter x:Name="oSplitter"></uc:Splitter>


my programm will not step into my OnSplitterColorChanged-handler. So my color will not be set to my oBorder Child.

What's the default way to set the default value of my dependency-property to my child ?

Jeff
Posted

1 solution

If you want to set the default value of a dependency-property to a child control, you can set it in the constructor of the UserControl.


But, why don't you use binding?


You can set the Background property of the child Border, like the following:


XML
<UserControl x:Class="DesktopApp.UserControls.Splitter"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Border x:Name="oBorder"
            Background="{Binding SplitterColor, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}}">
        <Border.Effect>
            <DropShadowEffect x:Name="oShadow" />
        </Border.Effect>
    </Border>
</UserControl>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900