【问题标题】:WPF Bind Textboxes Text On Usercontrol to Dependency but Use different Bindings on WindowWPF将用户控件上的文本框文本绑定到依赖项,但在窗口上使用不同的绑定
【发布时间】:2012-11-19 20:41:04
【问题描述】:

所以我在玩一些 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 上。但是现在整个结构开始变得奇怪了。 我无法更改数据上下文,否则用户控件上的绑定将不再起作用。

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

我需要依赖属性,以便我的控件的用户可以简单地绑定到新的“属性名称”,例如 LastName。

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

【问题讨论】:

    标签: c# .net wpf


    【解决方案1】:

    从用户控件中删除DataContext="{Binding RelativeSource={RelativeSource Self}}"&gt;,改为:

    &lt;Grid DataContext="{Binding ElementName=uc1}"&gt;.

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

    【讨论】:

    • 在某些时候,你看不到所有树木前面的森林。谢谢。解决了。不得不编辑anwser。它不是组合框,它们是文本框。 ;o)
    猜你喜欢
    • 2014-01-26
    • 2011-09-20
    • 2016-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多