WPF绑定文本框文本在Usercontrol上依赖,但在窗口上使用不同的绑定

时间:2012-11-19 20:41:05

标签: c# .net wpf

所以我正在玩一些WPF UserControls和DependencyProperties。 我有以下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>

使用此代码背后:

        #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设置为自己。

当我想在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中。但现在整个构造开始变得紧张。 我无法更改datacontext,因为否则usercontrol上的绑定将不再起作用。

我想我完全错过了解所有这一切应该如何运作的事情。

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

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

1 个答案:

答案 0 :(得分:2)

从用户控件中删除DataContext="{Binding RelativeSource={RelativeSource Self}}">,而不是:

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

同时从各个文本框中删除所有RelativeSource