2012-11-19 37 views
0

所以我正在玩弄一些WPF UserControls和DependencyProperties。 我有以下的用户控件:WPF将用户控件上的文本框文本绑定到依赖项,但在窗口上使用不同的绑定

<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文本属性绑定到后面代码中的dependencyproperties,我必须将datacontext设置为自身。

当我想现在在一个窗口中使用了这个控件我有一个问题:

 <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}"/> 

此属性存储上的应用程序的视图模型。但是现在整个构造开始变得枯燥。 我无法更改datacontext,因为否则usercontrol上的绑定将不再起作用。

我想我完全错误地理解了所有这些应该如何工作。

我需要依赖关系props,以便我的控件的用户可以简单地绑定到像LastName这样的新“属性名称”。

请有人解释我,我在哪里想错了?

回答

2

从用户控件,而是取出DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Grid DataContext="{Binding ElementName=uc1}">

也从个别文本框中删除所有RelativeSource

+0

在某些时候,你看不到所有树木的森林面前。谢谢。解决了。不得不编辑anwser。它不包含组合框,它们是文本框。 ; O) – luke

相关问题