Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing dependency property in the same control xaml

In a Wpf Application i have a main window. I have added a user control to the same project. In the user control's .xaml.cs file a Dependency property ( "Value" name of the property ) is added.

I would like to access the defined dependency property in the usercontrol.xaml. I know i can do the same while creating the control instance either in window.xaml or some other 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 mentioned my question wrongly. Nevertheless even i was not aware of accessing. But my actual intended question is it possible to set the dependency property from .xaml. some thing like from the example given above,

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

Or

<Grid CustomBackground ="Blue" />

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

like image 427
madhu sudhan Avatar asked Jul 04 '13 10:07

madhu sudhan


1 Answers

Yes it is possible.

something like:

.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);
    }
  }
}

Alternate:

If you say have the DataContext of the UserControl as itself like:

public UserControl1() {
  InitializeComponent();
  DataContext = this;
}

then in your xaml you could just go with:

<Grid Background="{Binding Path=DataContext.CustomBackground}" />

Update:

For the new question,

Not quite directly.

  • You can "set" the value if the custom DP is registered as an attached property(Do remember an attached property is not the same as a normal DP in it's behavior and scope.)
  • If you want to keep it as a normal DP, then you can keep UserControl1 from the original answer same as it is(just the DP part. You need to remove the xaml part of it and make it a non-partial class in the code-behind) and then derive it to a new UserControl.

something like:

<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 ofc name UserControl1 as something like "BaseUserControl" or so to make it obvious that it's not intended for direct usage.

  • You can set the value from the UserControl.Style in the same xaml as well.

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>
like image 110
Viv Avatar answered Sep 28 '22 15:09

Viv