Yes it is possible.
sort of:
.xaml
<UserControl x:Class="MvvmLight26.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MvvmLight26" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}, Path=CustomBackground}" /> </UserControl>
and .xaml.cs :
public partial class UserControl1 : UserControl { public static readonly DependencyProperty CustomBackgroundProperty = DependencyProperty.Register( "CustomBackground", typeof(Brush), typeof(UserControl1), new FrameworkPropertyMetadata(Brushes.Tomato)); public UserControl1() { InitializeComponent(); } public Brush CustomBackground { get { return (Brush)GetValue(CustomBackgroundProperty); } set { SetValue(CustomBackgroundProperty, value); } } }
Alternative:
If you say that DataContext UserControl as such:
public UserControl1() { InitializeComponent(); DataContext = this; }
then in your xaml you can just go with:
<Grid Background="{Binding Path=DataContext.CustomBackground}" />
Update:
For a new question
Not quite straight.
- You can “set” the value if the user DP is registered as an attached property (remember that the attached property does not match the regular DP in its behavior and scope.)
- If you want to save it as a regular DP, you can save
UserControl1 from the original response just like it (just part of DP). You need to remove the xaml part and make it a partial class in the code), and then output it to the new UserControl .
sort of:
<local:UserControl1 x:Class="MvvmLight26.UserControl2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MvvmLight26" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" CustomBackground="Blue" mc:Ignorable="d"> <Grid /> </local:UserControl1>
You can specify the name UserControl1 as something like "BaseUserControl" or so that it is obvious that it is not intended for direct use.
- You can set the value from
UserControl.Style in the same xaml file.
XAML:
<UserControl x:Class="MvvmLight26.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:MvvmLight26" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <UserControl.Style> <Style> <Setter Property="local:UserControl1.CustomBackground" Value="Blue" /> </Style> </UserControl.Style> <Grid Background="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:UserControl1}}, Path=CustomBackground}" /> </UserControl>
source share