Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# UserControl属性的默认值_C#_Wpf_User Controls - Fatal编程技术网

C# UserControl属性的默认值

C# UserControl属性的默认值,c#,wpf,user-controls,C#,Wpf,User Controls,UserControl包含从Control派生的BorderBrush属性。我如何设置它的默认值,例如,设置为brushs.Black,并让使用我的控件的开发人员可以设置它 我试图在控件的xaml文件及其构造函数的标记中指定初始值,但当我执行任何操作时,都会为控件指定值 外部被忽略。您通常会通过覆盖UserControl派生类中的BorderBrush属性的元数据来完成此操作: public partial class MyUserControl : UserControl { stat

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

我试图在控件的xaml文件及其构造函数的
标记中指定初始值,但当我执行任何操作时,都会为控件指定值
外部被忽略。

您通常会通过覆盖UserControl派生类中的
BorderBrush
属性的元数据来完成此操作:

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

    public MyUserControl()
    {
        InitializeComponent();
    }
}

也许风格最好。您可以创建一个新的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"/>

会工作吗?@Sometowngeek dunno,现在无法检查,因为一年前更改为aspnet developer