Conditional dependency property in one xaml control

In a Wpf application, I have a main window. I added a user control to the same project. The Dependency property ("Value" of the property property) is added to the .xaml.cs user control.

I would like to access a specific dependency property in usercontrol.xaml. I know that I can do the same by creating an instance of the control either in window.xaml or in another user control.

But is it possible to access the dependency property defined in .xaml.cs in .xaml?

Question updated based on Vivs answer

Ok I incorrectly mentioned my question. However, even I did not know about access. But my actual question is that you can set the dependency property from .xaml. something like the example above

<Grid CustomBackground ="{Binding Path= BackgroundColor}" /> 

or

 <Grid CustomBackground ="Blue" /> 

Is it possible to configure dependency properties like this in the same .xaml?

+4
source share
1 answer

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> 
+9
source

Source: https://habr.com/ru/post/1489681/


All Articles