WPF用户控件-将DependencyProperty的默认值设置为Child [英] WPF Usercontrol - setting Default-Value of DependencyProperty to Child
问题描述
嗨社区,
我创建了一个用户控件"Splitter".
xaml看起来像这样:
Hi Community,
I''ve created an usercontrol ''Splitter''.
xaml looks like this:
<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>
我想访问oBorder和oShadow-Childs的某些属性.
例如,我要像这样设置子级"oBorder"的颜色属性:
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:
<uc:Splitter x:Name="oSplitter" SplitterColor="Orange"></uc:Splitter>
uc定义如下:
where uc is defined as following:
xmlns:uc="clr-namespace:DesktopApp.UserControls"
因此,我已将名为"SplitterColor"的DP放入我的用户控件.
So I''ve put a DP called ''SplitterColor'' into my usercontrol.
public Brush SplitterColor
{
get { return (Brush)GetValue(SplitterColorProperty); }
set { SetValue(SplitterColorProperty, value); }
}
我已定义默认值"Brushes.White"
I''ve defined a default value ''Brushes.White''
public static readonly DependencyProperty SplitterColorProperty =
DependencyProperty.Register("SplitterColor", typeof(Brush), typeof(Splitter), new PropertyMetadata(Brushes.White, OnSplitterColorChanged));
这段代码更改了oBorder Child的颜色属性.在此示例中,dNewBackgroundColor将是橙色画笔.
This code changes the Color-Property of by oBorder Child. In this example dNewBackgroundColor will be an Orange brush.
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:
<uc:Splitter x:Name="oSplitter" SplitterColor="White"></uc:Splitter>
或
or
<uc:Splitter x:Name="oSplitter"></uc:Splitter>
我的程序不会进入我的OnSplitterColorChanged-handler.所以我的颜色不会设置为我的oBorder Child.
将我的依赖项属性的默认值设置给孩子的默认方法是什么?
Jeff
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
推荐答案
如果要为子控件设置依赖项属性的默认值,则可以在
.
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?
您可以设置子级Border
的Background
属性,如下所示:
You can set the Background
property of the child Border
, like the following:
<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>
这篇关于WPF用户控件-将DependencyProperty的默认值设置为Child的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!