【问题标题】:Default value for property of UserControlUserControl 属性的默认值
【发布时间】:2015-07-23 11:01:27
【问题描述】:

UserControl 包含从Control 派生的BorderBrush 属性。如何设置它的默认值,例如,Brushes.Black 并使其可供使用我的控件的开发人员设置?

我试图在控件的 xaml 文件和它的构造函数中的 <UserControl> 标记中分配初始值,但是当我这样做时,为控件分配的值 外部被忽略。

【问题讨论】:

标签: c# wpf user-controls


【解决方案1】:

您通常会通过在 UserControl 派生类中覆盖 BorderBrush 属性的元数据来做到这一点:

public partial class MyUserControl : UserControl
{
    static MyUserControl()
    {
        BorderBrushProperty.OverrideMetadata(
            typeof(MyUserControl),
            new FrameworkPropertyMetadata(Brushes.Black));
    }

    public MyUserControl()
    {
        InitializeComponent();
    }
}

【讨论】:

    【解决方案2】:

    也许样式是最好的。您可以创建一个新的 UserControl,我们称之为 BorderedControl。我创建了一个名为 Controls 的新文件夹来保存它。

    <UserControl x:Class="BorderTest.Controls.BorderedControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
    
    </Grid>
    </UserControl>
    

    接下来,创建一个资源字典,UserControlResources。一定要包含控件的命名空间:

    <ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ctrls="clr-namespace:BorderTest.Controls">
        <Style TargetType="{x:Type ctrls:BorderedControl}">
            <Setter Property="BorderBrush" Value="Lime"/>
            <Setter Property="BorderThickness" Value="3"/>
        </Style>
    </ResourceDictionary>
    

    在这里你可以设置你想要的默认属性。

    然后,在用户控制资源中包含资源字典:

    <UserControl x:Class="BorderTest.Controls.BorderedControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <ResourceDictionary Source="/BorderTest;component/Resources/UserControlResources.xaml"/>
    </UserControl.Resources>
    <Grid>
    
    </Grid>
    </UserControl>
    

    最后,将控件添加到主窗口:

    <Window x:Class="BorderTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:ctrls="clr-namespace:BorderTest.Controls"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <ctrls:BorderedControl Width="100"
                               Height="100"/>
    </Grid>
    </Window>
    

    这是我的解决方案:

    这是您运行时的应用程序:

    你可以简单地改变你的用户控件的边框:

    <ctrls:BorderedControl Width="100"
                           Height="100"
                           BorderBrush="Orange"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-18
      • 1970-01-01
      • 2023-03-18
      • 2011-12-16
      • 2010-10-16
      • 2011-02-05
      • 2013-05-30
      • 2012-08-07
      相关资源
      最近更新 更多