Binding DependencyProperty in nested user controls - c#

I've this problem: I've made 2 user controls: NestedControl1 and NestedControl2. NestedControl2 contains NestedControl1, and NestedControl1 contains just a TextBlock.
I've set each NestedControl* DataContext to Self, and created a dependency property for each one.
NestedControl1.MyText1 and NestedControl2.MyText2.
Then I've bound the NestedControl2.MyText1 to MyText2, and the TextBlock.Text to MyText1 .
If I use the NestedControl2 on a Window and set MyText2 to whatever, it does not work. However if I use directly the NestedControl1 on a Window, it does work. The point is that I would like to make the value of MyText2 arrive to the TextBlock.Text property inside of NestedControl1 .
The code is the following.. What's wrong?? Any idea?? Thank tou in advance for the answers
NestedControl2 code:
public partial class NestedControl2 : UserControl
{
public NestedControl2()
{
InitializeComponent();
}
public static readonly DependencyProperty MyText2Property = DependencyProperty.Register(
"MyText2", typeof(string), typeof(NestedControl2), new PropertyMetadata(default(string)));
public string MyText2
{
get { return (string)GetValue(MyText2Property); }
set { SetValue(MyText2Property, value); }
}
}
NestedControl2 xaml:
<UserControl x:Class="TestNestedPropertiesWpf.NestedControl2"
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"
xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid>
<testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}" />
</Grid>
NestedControl1 code:
public partial class NestedControl1 : UserControl
{
public NestedControl1()
{
InitializeComponent();
}
public static readonly DependencyProperty MyText1Property = DependencyProperty.Register(
"MyText1", typeof(string), typeof(NestedControl1), new PropertyMetadata(default(string)));
public string MyText1
{
get { return (string)GetValue(MyText1Property); }
set { SetValue(MyText1Property, value); }
}
}
NestedControl1 xaml:
<UserControl x:Class="TestNestedPropertiesWpf.NestedControl1"
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"
xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<TextBlock Text="{Binding MyText1}"
x:Name="textBlock" Foreground="Red"
Width="300" Height="100" Background="Black"></TextBlock>
</Grid>
And in the end, this is MainWindow.xaml:
<Window x:Class="TestNestedPropertiesWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:testNestedPropertiesWpf="clr-namespace:TestNestedPropertiesWpf"
Title="MainWindow" Height="350" Width="525" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<StackPanel>
<testNestedPropertiesWpf:NestedControl1 MyText1="WORKING"/>
<testNestedPropertiesWpf:NestedControl2 MyText2="NOT WORKING"/>
</StackPanel>

This won't work:
testNestedPropertiesWpf:NestedControl1 MyText1="{Binding MyText2}"
add a clr property to deliver the value from one DP to another:
add a .Net property "MyBindableText" to NestedControl2, make the DP MyText1 bind to it.
add an OnPropertyChanged handler in the MyText2 DP's registration. In this handler, assign the new value of the DP to "MyBindableText"
Now If you set MyText2="bla", the value is forwarded to Mytext1, and will be set for both DP's MyText1 and MyText2

I found the solution. The NestedControl2 had DataContext to self, and used NestedControl1, wich had DataContext to self, so the two DataContext were different.
To solve the problem I modified the binding declared in the MyText1 like this:
<testNestedPropertiesWpf:NestedControl1 MyText1="{Binding RelativeSource={RelativeSource AncestorType={x:Type testNestedPropertiesWpf:NestedControl2}}, Path=MyText2}" />

Related

How to get custom property on MainWindow into a UserControl (from UserControl library/DLL)?

I have a set of UserControls in a library, but because the library is in a different namespace than the MainWindow, I don't seem to be able to get one UserControl to retrieve List<features> from MainWindow.
I suspect this is because UserControl does not know of MainWindow, and it's not meant to, as it is in a DLL library. As the UserControl is in a DLL, it should be agnostic to namespaces, but still be able to get what it needs.
So below I put some XAML and relative C# code-behind where you can see on the UserControls ListBox, I'm trying to retrieve the features list from MainWindow.
<UserControl x:Class="FlatControls.MyListBox"
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"
xmlns:local="clr-namespace:FlatControls"
mc:Ignorable="d"
d:DesignHeight="34" d:DesignWidth="100" MaxHeight="34" MaxWidth="100" x:Name="root">
<ListBox x:Name="listBox">
</UserControl>
public MyListBox()
{
InitializeComponent();
listBox.Items = ???????????
}
<Window x:Class="MyApp.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
xmlns:FC="clr-namespace:FlatControls;assembly=FlatControls"
mc:Ignorable="d"
Title="MainWindow" Height="1000" Width="1900" WindowState="Maximized" RenderOptions.BitmapScalingMode="Fant">
<Grid>
xyz
</Grid>
<Window>
namespace MyApp
{
public List<string> features = new List<string>();
public MainWindow()
{
InitializeComponent();
features.Add("Concave");
features.Add("Convex");
}
}
Any help would be greatly appreciated, whether it's via Binding, or code-behind :D
The ListBox in your custom UserControl has to bind its ItemsSource from somewhere. You should create an ItemsSource dependency property in your MyListBox to enable binding a collection from outside.
public partial class MyListBox : UserControl
{
public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
nameof(ItemsSource), typeof(IEnumerable), typeof(MyListBox), new PropertyMetadata());
public IEnumerable ItemsSource
{
get => (IEnumerable)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public MyListBox()
{
InitializeComponent();
}
}
In the markup for the MyListBox user control, bind to this property using a RelativeSource binding.
<UserControl x:Class="FlatControls.MyListBox"
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"
xmlns:local="clr-namespace:FlatControls"
mc:Ignorable="d"
d:DesignHeight="34" d:DesignWidth="100" MaxHeight="34" MaxWidth="100" x:Name="root">
<ListBox x:Name="listBox" ItemsSource="{Binding ItemsSource, RelativeSource={RelativeSource AncestorType={x:Type local:MyListBox}}}"/>
</UserControl>
Now, in your MainWindow use the control (you already added the corresponding XML namespace).
<Window x:Class="MyApp.MainWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp4"
xmlns:FC="clr-namespace:FlatControls;assembly=FlatControls"
mc:Ignorable="d"
Title="MainWindow" Height="1000" Width="1900" WindowState="Maximized" RenderOptions.BitmapScalingMode="Fant">
<Grid>
<FC:MyListBox x:Name="MyListBox"/>
</Grid>
<Window>
Finally, assign the ItemsSource property of the MyListBox control with the features collection.
public MainWindow()
{
InitializeComponent();
features.Add("Concave");
features.Add("Convex");
MyListBox.ItemsSource = Features;
}
Alternatively, expose a property Features for your items and bind it in XAML.
public List<string> Features { get; }
<FC:MyListBox ItemsSource="{Binding Features}"/>
As #thatguy said, you can use his way to do.
Of course, if you like to write code in view.cs, you can use "FieldModifier" word to decorade you listbox in your MyListBox:
<ListBox x:Name="listBox" x:FieldModifier="public" />
And in your MainWindow, you can get the listbox instance, of cource, you can set its itemsource like this:
public MainWindow()
{
InitializeComponent();
this.userControl.listBox.ItemsSource = new List<string>() { "11","22","33"};
}

Navigation from one view to another in WPF MVVM

I wrote code which should navigate between user controls in WPF application using MVVM, but I realised that this code doesn't work.
From window LoginView I want to change the view to VotingCardView.
Actually, after clicking on the button in the LoginView, the method DisplayVCV gets executed, but the view is not going to change. What am I doing wrong?
MainView.xaml:
<Window x:Class="ElectionCalculator.View.MainView"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ElectionCalculator"
xmlns:v="clr-namespace:ElectionCalculator.View"
xmlns:vm="clr-namespace:ElectionCalculator.ViewModel"
mc:Ignorable="d"
Title="Election calculator" Height="350" Width="525">
<Window.DataContext>
<vm:MainViewModel />
</Window.DataContext>
<ContentControl Content="{Binding ViewModel}" />
</Window>
LoginView.xaml:
<UserControl x:Class="ElectionCalculator.View.LoginView"
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"
xmlns:local="clr-namespace:ElectionCalculator.View"
xmlns:vm="clr-namespace:ElectionCalculator.ViewModel"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Button Command="{Binding DataContext.DisplayVC, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=OneWay}" Margin="161,147,47,124" />
</Grid>
</UserControl>
MainViewModel.cs
class MainViewModel : BaseViewModel
{
public BaseViewModel ViewModel { get; set; }
public MainViewModel()
{
ViewModel = new LoginViewModel();
}
public ICommand DisplayVC { get { return new RelayCommand(DisplayVCV); } }
public void DisplayVCV()
{
ViewModel = new VotingCardViewModel();
MessageBox.Show("DisplayVCCommandExecuted");
}
}
Your ViewModel property implementation doesn't raise a PropertyChanged event when the value changes. This is usually done via an INotifyPropertyChanged implementation. Because of that, your view doesn't get notified that something has changed.
In your case, this means that you need a backing field for your ViewModel property and implement your ViewModel property similar to this:
private BaseViewModel _viewModel;
public BaseViewModel ViewModel
{
get { return _viewModel; }
set
{
if(_viewModel != value)
{
_viewModel = value;
OnPropertyChanged("ViewModel");
}
}
}
Since you are already deriving from BaseViewModel I assume that the method OnPropertyChanged (or some method with a similar name) is implemented there. It is also quite common that you don't have to specify the property name ("ViewModel") as an argument, since lots of implementations use the [CallerMemberName] attribute for this purpose.

Bind DependencyProperty of Usercontrol in ListBox

I need ListBox with my UserControl listed in it. My UserControl has TextBox. So I want to display property of List's subitem in UserControl's textBox. I have tried a lot of options with DataContext and ElementName - it just doesn`t work. I just stucked on it. The only way to make it work is to remove DataContext binding of UserControl to itself and change Item Property name so it matches to DependencyProperty name - but I need to reuse my control in different viewmodels with different entities so it is almost not possible to use the approach.
Interesting thing is that if I change my UserControl to Textbox and bind Text property of it - everything works. What the difference between Textbox and my UserControl?
So let me just show my code.
I have simplified the code to show only essential:
Control XAML:
<UserControl x:Class="TestControl.MyControl"
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="100" d:DesignWidth="200"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<TextBlock Text="{Binding Text}"/>
</Grid>
</UserControl>
Control CS:
public partial class MyControl : UserControl
{
public MyControl()
{
InitializeComponent();
}
public string Text
{
get {
return (string)this.GetValue(TextProperty); }
set {
this.SetValue(TextProperty, value); }
}
public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(MyControl), new propertyMetadata(""));
}
Window XAML:
<Window x:Class="TestControl.MainWindow"
Name="_windows"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestControl"
Title="MainWindow" Height="350" Width="525" >
<Grid Name="RootGrid">
<ListBox ItemsSource="{Binding ElementName=_windows, Path=MyList}">
<ItemsControl.ItemTemplate >
<DataTemplate >
<local:MyControl Text="{Binding Path=Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</Grid>
</Window>
Window CS:
public partial class MainWindow : Window
{
public MainWindow()
{
_list = new ObservableCollection<Item>();
_list.Add(new Item("Sam"));
_list.Add(new Item("App"));
_list.Add(new Item("H**"));
InitializeComponent();
}
private ObservableCollection<Item> _list;
public ObservableCollection<Item> MyList
{
get { return _list;}
set {}
}
}
public class Item
{
public Item(string name)
{
_name = name;
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
}
This is a pretty big gotcha in XAML. The problem is that when you do this in the user control:
DataContext="{Binding RelativeSource={RelativeSource Self}}"
You change its data context, so that in this line:
<local:MyControl Text="{Binding Path=Name}"/>
The runtime will now attempt to resolve "Name" on the instance of "MyControl", instead of on the inherited data context (ie, the view model). (Confirm this by checking the Output window -- you should see a binding error to that effect.)
You can get around this by, instead of setting the user control's data context that way, using a RelativeSource binding:
<UserControl x:Class="TestControl.MyControl"
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="100" d:DesignWidth="200"
<Grid>
<TextBlock
Text="{Binding Text,RelativeSource={RelativeSource AncestorType=UserControl}}"
/>
</Grid>
</UserControl>

How to Bind to a Custom Controls Button Visibility from Within Another Control

I have a custom control, which has a button:
<UserControl x:Class="Gambit.Views.FileSelectionControl"
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"
SnapsToDevicePixels="True"
mc:Ignorable="d">
...
<Button Content="Load"
Margin="5,5,5,5"
Height="22"
Width="70"
IsDefault="True"
IsEnabled="{Binding SelectedFileExists}"
AttachedCommand:CommandBehavior.Event="Click"
AttachedCommand:CommandBehavior.Command="{Binding CloseDialogCommand}"/>
...
</UserControl>
I want to include this control, in another control, but I want to set the Load buttons visibility at design time in the host control; something like
<UserControl x:Class="Gambit.Views.SomeOtherControl"
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"
SnapsToDevicePixels="True"
mc:Ignorable="d">
...
<GroupBox Header="Select Test Data">
<Views:FileSelectionControl <Here Set the Load Button Visibility>/>
</GroupBox>
...
</UserControl>
where <Here Set the Load Button Visibility> shows where i want to set the visibility of the control. How is this done [without breaking the MVVM pattern]?
Thanks for your time.
You can create DependencyProperty in your UserControl:
public partial class SomeView : UserControl
{
...
public static DependencyProperty ButtonVisibilityProperty = DependencyProperty.Register("ButtonVisibility", typeof(Visibility), typeof(SomeView));
public Visibility ButtonVisibility
{
get { return (Visibility)GetValue(ButtonVisibilityProperty); }
set { SetValue(ButtonVisibilityProperty, value); }
}
}
bind it to Button.Visibility:
<UserControl x:Class="WpfApplication2.SomeView"
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">
<Button Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}}, Path=ButtonVisibility}" Content="My Button"/>
</UserControl>
and then you can control Visibility from outside like so:
<local:SomeView ButtonVisibility="Collapsed"/>
and because it's a DependencyProperty you can use Binding as well
Hi just create a bool or Visibility Type property in UserControl1 and set it in Usercontrol2 like
UserControl1 xaml
<UserControl x:Class="WpfApplication4.UserControl1"
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>
<Button x:Name="Loadbutton" Content="load"/>
</Grid>
xaml.cs
public UserControl1()
{
InitializeComponent();
}
bool showLoadButton;
public bool ShowLoadButton
{
get { return showLoadButton; }
set
{
showLoadButton = value;
if (showLoadButton)
Loadbutton.Visibility = Visibility.Visible;
else
Loadbutton.Visibility = Visibility.Collapsed;
}
}
UserControl2 Set ShowLoadButton True or false
<UserControl x:Class="WpfApplication4.UserControl2"
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"
xmlns:local="clr-namespace:WpfApplication4"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<local:UserControl1 ShowLoadButton="True"/>
</Grid>
If you do not want to define a property in the UserControl, which you can always create attached dependency property, and you can declare it in a separate class under the common namespace.
Something like this:
MainWindow.xaml
<local:TestUserControl AttachedProperties:ButtonExt.Visibility="Visible" />
TestUserControl.xaml
<Button Visibility="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
Path=(AttachedProperties:ButtonExt.Visibility)}"
Content="TestButton" />
Attached property definition:
public static class ButtonExt
{
public static readonly DependencyProperty VisibilityProperty;
public static void SetVisibility(DependencyObject DepObject, Visibility value)
{
DepObject.SetValue(VisibilityProperty, value);
}
public static Visibility GetVisibility(DependencyObject DepObject)
{
return (Visibility)DepObject.GetValue(VisibilityProperty);
}
static ButtonExt()
{
PropertyMetadata VisibiltyPropertyMetadata = new PropertyMetadata(Visibility.Collapsed);
VisibilityProperty = DependencyProperty.RegisterAttached("Visibility",
typeof(Visibility),
typeof(ButtonExt),
VisibiltyPropertyMetadata);
}
}
Some notes about code-behind in MVVM
I agree with #dkozl, his example does not violate the principle of MVVM, in some cases, the code is present in the View, for example (personally, I always try to avoid code-behind):
Installation DataContext.
The use of different patterns such as Mediator, Proxy, etc.
Determination of properties and behaviors that pertain only to View (as in your case).
The most important thing when you use the code-behind, it is that all actions possible through ViewModel occurred, ie in ViewModel contains all the logic and for example, in the View click event, call the function, which is in ViewModel.
For more information about code-behind, please see the answers to the recent question:
WPF MVVM Code Behind

Custom usercontrol property binding failure silverlight

I have a custom usercontrol with DataContext="{Binding RelativeSource={RelativeSource self}}"
On the code behind i've made a dependency property like:
public static DependencyProperty ElementNameProperty = DependencyProperty.Register("ElementName",
typeof(string),
typeof(ElementControl),
new PropertyMetadata(new PropertyChangedCallback((s, e) => { new Base().OnPropertyChanged("ElementName"); })));
public string ElementName
{
get
{
return (string)base.GetValue(ElementNameProperty);
}
set
{
base.SetValue(ElementNameProperty, value);
}
}
Now when I try to use this usercontrol in my mainpage.xaml and use the following binding: <test.TestControl ElementName="{Binding name}" />, it keeps searching for 'name' property in my custom usercontrol instead of where it should come from?
What am I doing wrong ?
It searches there because you have the DataContext set on the topmost level for your user control. What you would need to do is get rid of the relative binding to self in the user control and specify ElementName in bindings (inside user control). Btw you probably don't need OnPropertyChanged in the PropertyChangedCallback cause DependencyProperties in their nature notify about value changes.
I eventually solved it this way. Not the way I wanted, but it's a (in my eyes) pretty neat solution.
CustomUserControl.xaml
<UserControl x:Class="TestApp.Controls.CustomUserControl"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Width="75"
Height="75">
<Canvas x:Name="LayoutRoot"
Background="Black">
<StackPanel Orientation="Vertical">
<Image x:Name="UCImage"
Width="50"
Height="50"
HorizontalAlignment="Center" />
<TextBlock x:Name="UCText"
HorizontalAlignment="Center" />
</StackPanel>
</Canvas>
</UserControl>
CustomUserControl.xaml.cs
public partial class ElementControl : UserControl
{
#region DependencyProperty ElementNameProperty
public static DependencyProperty ElementNameProperty = DependencyProperty.Register("ElementName",
typeof(string),
typeof(ElementControl),
new PropertyMetadata(new PropertyChangedCallback((s, e) =>
{
//See Here
((ElementControl)s).UCText.Text = e.NewValue as string;
})));
public string ElementName
{
get
{
return (string)base.GetValue(ElementNameProperty);
}
set
{
base.SetValue(ElementNameProperty, value);
}
}
#endregion
}

Categories

Resources