Reputation: 2514
UserControl
contains BorderBrush
property derived from Control
. How can I setup its default value, for example, to Brushes.Black
and make it available to be set by developer who will use my control?
I tried to assign initial value in <UserControl>
tag in control's xaml file and in constructor of it, but when I do any of this, value assigned for the control
externally is ignored.
Upvotes: 5
Views: 3028
Reputation: 123
Perhaps styles would be best. You could create a new UserControl, let's call it BorderedControl. I've created a new folder called Controls to hold it.
<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>
Next, create a Resource Dictionary, UserControlResources. Be sure to include the namespace of the control:
<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>
Here you can set what ever properties you'd like to have default.
Then, include the resource dictionary in the user control resources:
<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>
Finally, add the control to your main window:
<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>
Here is my solution:
Here is the application when you run it:
You can simply change the border of your user control with this:
<ctrls:BorderedControl Width="100"
Height="100"
BorderBrush="Orange"/>
Upvotes: 3
Reputation: 128013
You would usually do this by overriding metadata for the BorderBrush
property in your UserControl derived class:
public partial class MyUserControl : UserControl
{
static MyUserControl()
{
BorderBrushProperty.OverrideMetadata(
typeof(MyUserControl),
new FrameworkPropertyMetadata(Brushes.Black));
}
public MyUserControl()
{
InitializeComponent();
}
}
Upvotes: 8