Reputation: 752
So I'm playing around with some WPF UserControls and DependencyProperties. I've got the following UserControl:
<UserControl x:Class="ValueInser.UserControls.FirmPersonInsert"
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:self="clr-namespace:ValueInser.UserControls"
x:Name="uc1"
mc:Ignorable="d"
d:DesignHeight="225" d:DesignWidth="450"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100px"/>
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="40"/>
<RowDefinition IsEnabled="{Binding ElementName=rbtnPers, Path=IsChecked}"/>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<GroupBox Grid.Column="1" Header="Privat-/Juristische Person" Width="150" HorizontalAlignment="Left">
<StackPanel Orientation="Horizontal">
<RadioButton Content="Person" IsChecked="{Binding Path=IsCompany}"/>
<Line Height="1" Width="25" />
<RadioButton Content="Firma" Name="rbtnCompany" />
</StackPanel>
</GroupBox>
<Label Content="Firma" Grid.Column="0" Grid.Row="1" />
<TextBox Grid.Column="1" Grid.Row="1" Text="{Binding Path=Company}" IsEnabled="{Binding ElementName=rbtnCompany, Path=IsChecked}"/>
<Label Content="Anrede" Grid.Column="0" Grid.Row="2" />
<TextBox Grid.Column="1" Grid.Row="2" Text="{Binding Path=Salutation}" />
<Label Content="Name" Grid.Column="0" Grid.Row="3" />
<TextBox Grid.Column="1" Grid.Row="3" Text="{Binding Path=LastName, RelativeSource={RelativeSource self}}" />
<Label Content="Vorname" Grid.Column="0" Grid.Row="4" />
<TextBox Grid.Column="1" Grid.Row="4" Text="{Binding Path=FirstName}"/>
<Label Content="Strasse" Grid.Column="0" Grid.Row="5" />
<TextBox Grid.Column="1" Grid.Row="5" Text="{Binding Path=Street}" />
<Label Content="Ort" Grid.Column="0" Grid.Row="6" />
<TextBox Grid.Column="1" Grid.Row="6" Text="{Binding Path=City}" />
<Label Content="PLZ" Grid.Column="0" Grid.Row="7" />
<TextBox Grid.Column="1" Grid.Row="7" Text="{Binding Path=ZIP}" />
</Grid>
With this Code Behind:
#region Dependency Properties
public static readonly DependencyProperty _company = DependencyProperty.Register("Company", typeof(string), typeof(UserControl));
public string Company
{
get
{
return this.GetValue(_company) as string;
}
set
{
this.SetValue(_company, value);
}
}
public static readonly DependencyProperty _salutation = DependencyProperty.Register("Salutation", typeof(string), typeof(UserControl));
public string Salutation
{
get
{
return this.GetValue(_salutation) as string;
}
set
{
this.SetValue(_salutation, value);
}
}
public static readonly DependencyProperty _lastName = DependencyProperty.Register("LastName", typeof(string), typeof(UserControl));
public string LastName
{
get
{
return this.GetValue(_lastName) as string;
}
set
{
this.SetValue(_lastName, value);
}
}
public static readonly DependencyProperty _firstName = DependencyProperty.Register("FirstName", typeof(string), typeof(UserControl));
public string FirstName
{
get
{
return this.GetValue(_firstName) as string;
}
set
{
this.SetValue(_firstName, value);
}
}
public static readonly DependencyProperty _street = DependencyProperty.Register("Street", typeof(string), typeof(UserControl));
public string Street
{
get
{
return this.GetValue(_street) as string;
}
set
{
this.SetValue(_street, value);
}
}
public static readonly DependencyProperty _city = DependencyProperty.Register("City", typeof(string), typeof(UserControl));
public string City
{
get
{
return this.GetValue(_city) as string;
}
set
{
this.SetValue(_city, value);
}
}
public static readonly DependencyProperty _zip = DependencyProperty.Register("ZIP", typeof(string), typeof(UserControl));
public string ZIP
{
get
{
return this.GetValue(_zip) as string;
}
set
{
this.SetValue(_zip, value);
}
}
public static readonly DependencyProperty _isCompany = DependencyProperty.Register("IsCompany", typeof(bool), typeof(UserControl));
public bool IsCompany
{
get
{
return !(bool)this.GetValue(_isCompany);
}
set
{
this.SetValue(_isCompany, value);
}
}
#endregion
And now the tricky part comes to play. So that I can bind the textboxs text property to the dependencyproperties in the code behind I have to set the datacontext to itself.
When I want to use this control now on a Window I have a problem:
<uc:FirmPersonInsert DataContext="{DynamicResource ResourceKey=mvm}"
IsCompany="{Binding Path=ProjectArchitect.IsCompany}"
Company="{Binding Path=ProjectArchitect.Company}"
LastName="{Binding Path=ProjectArchitect.LastName}"
FirstName="{Binding Path=ProjectArchitect.FirstName}"
Street="{Binding Path=ProjectArchitect.Street}"
City="{Binding Path=ProjectArchitect.City}"
ZIP="{Binding Path=ProjectArchitect.ZIP}"/>
This Properties are stored on the ViewModel of the Application. But now the whole construct starts to get weired. I can't change the datacontext because otherwise the bindings on the usercontrol won't work anymore.
I think I missunderstand something completly how all of that should work.
I need the dependencyprops so that a user of my control can simply bind to the new "Property Names" like LastName.
Please can someone explain me, where am I thinking wrong ?
Upvotes: 1
Views: 2816
Reputation: 44038
Remove the DataContext="{Binding RelativeSource={RelativeSource Self}}">
from the usercontrol and instead:
<Grid DataContext="{Binding ElementName=uc1}">
.
Also remove all the RelativeSource
s from the individual textboxes.
Upvotes: 3