所以我在玩一些 WPF UserControls 和 DependencyProperties。我有以下用户控件:
<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>
使用此代码:
#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
现在棘手的部分开始发挥作用。这样我就可以将 textboxs 文本属性绑定到后面代码中的依赖属性,我必须将 datacontext 设置为自身。
当我现在想在 Window 上使用这个控件时,我遇到了一个问题:
<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}"/>
此属性存储在应用程序的 ViewModel 上。但是现在整个结构开始变得奇怪了。我无法更改数据上下文,否则用户控件上的绑定将不再起作用。
我想我完全误解了所有这些应该如何工作的东西。
我需要dependencyprops,以便我的控件的用户可以简单地绑定到新的“属性名称”,如LastName。
请有人可以解释一下,我在哪里想错了?