2017-06-05 131 views
0

我創建了一個具有標籤和文本框的用戶控件。 我添加了兩個DependencyProperties(文本和標籤)並將它們綁定到textbox.text和label.content。但是,我無法看到文本框的文本。自定義用戶控件不綁定數據

在主窗口,當我沒有綁定到任何元素的標籤顯示,但如果我綁定元素不顯示。該文本框不顯示任何方式。

這裏的XAML:

<UserControl x:Class="TestNewLabeltextbox.UserControl1" 
     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" 
     mc:Ignorable="d" 
     d:DesignHeight="300" d:DesignWidth="300"> 
<StackPanel Orientation="Horizontal" Background="White" FlowDirection="RightToLeft"> 
    <Label x:Name="lbl" Content="{Binding Label, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="100" HorizontalAlignment="Left" Background="blue"> 
     <Label.Style> 
      <Style TargetType="Label"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="Label"> 
          <StackPanel Orientation="Horizontal"> 
           <Border Background="Blue" Width="200" BorderThickness="0,0,0,0"> 
            <StackPanel Orientation="Horizontal"> 
             <Viewbox StretchDirection="DownOnly" Stretch="Uniform"> 
              <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" TextBlock.FontSize="14" TextBlock.Foreground="#FFFFFF" Margin="5"> 
               <ContentPresenter.Effect> 
                <DropShadowEffect BlurRadius="0.0" 
               Color="#032A6B" 
               Direction="90" 
               Opacity="1" 
               ShadowDepth="1" /> 
               </ContentPresenter.Effect> 
              </ContentPresenter> 
             </Viewbox> 
            </StackPanel> 
           </Border> 
          </StackPanel> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </Label.Style> 
    </Label> 
    <TextBox x:Name="txt" Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Width="120" HorizontalAlignment="Right"> 
     <TextBox.Style> 
      <Style TargetType="TextBox"> 
       <Setter Property="Template"> 
        <Setter.Value> 
         <ControlTemplate TargetType="TextBox"> 
          <Border CornerRadius="0,0,0,50" BorderBrush="Black" Background="White" BorderThickness="0"> 
           <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center" RecognizesAccessKey="True" TextBlock.FontSize="14" TextBlock.Foreground="#FFFFFF" Margin="5"> 
            <ContentPresenter.Effect> 
             <DropShadowEffect BlurRadius="0.0" 
               Color="#032A6B" 
               Direction="90" 
               Opacity="1" 
               ShadowDepth="1" /> 
            </ContentPresenter.Effect> 
           </ContentPresenter> 
          </Border> 
         </ControlTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
     </TextBox.Style> 
    </TextBox> 
</StackPanel> 

Here'sUserControl1.cs:

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 
     this.DataContext = this; 
    } 

    public override void OnApplyTemplate() 
    { 
     base.OnApplyTemplate(); 
    } 

    public static readonly DependencyProperty LabelProperty = DependencyProperty.Register("Label", typeof(string), typeof(UserControl1), new PropertyMetadata(null)); 
    public string Label 
    { 
     get { return (string)this.GetValue(LabelProperty); } 
     set { this.SetValue(LabelProperty, value); } 
    } 

    public static readonly DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(string), typeof(UserControl1), new PropertyMetadata(null)); 
    public string Text 
    { 
     get { return (string)this.GetValue(TextProperty); } 
     set { this.SetValue(TextProperty, value); } 
    } 
} 

這裏的窗口XAML + CS:

<Window x:Class="TestNewLabeltextbox.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:controls="clr-namespace:TestNewLabeltextbox" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <StackPanel Orientation="Vertical" Height="150"> 
     <controls:UserControl1 Text="hello" Height="50" Label="{Binding Hello, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /> 
     <controls:UserControl1 Text="hello" Height="50" Label="world" /> 
     <Label BorderBrush="Black" BorderThickness="2" Width="100" Height="50" Content="{Binding Hello, Mode=TwoWay}"/> 
    </StackPanel> 
</Grid> 

public partial class MainWindow : Window 
{ 
    ViewModel vm = new ViewModel(); 
    public MainWindow() 
    { 
     InitializeComponent(); 
     vm.Hello = "555"; 
     this.DataContext = vm;    
    } 
} 

viewmodel.cs

public class ViewModel : INotifyPropertyChanged 
{ 
    private string h = "Hello"; 
    public string Hello 
    { 
     get 
     { 
      return h; 
     } 
     set 
     { 
      h = value; 
      NotifyPropertyChanged("Hello"); 
     } 
    } 

    #region "PropertyChanged Event" 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void NotifyPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 
    #endregion 
} 

回答

0

結合默認SourceDataContext。但是您的LabelText依賴項屬性在控件中定義而不是在視圖模型中定義。更改爲

{Binding Path=Label, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}} 

Label結合並

{Binding Path=Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type UserControl}}, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged} 

TextBox結合請閱讀ModeUpdateSourceTriggerBinding性能。看來你不知道他們是如何工作的。 Mode=TwoWay, UpdateSourceTrigger=PropertyChangedContent屬性沒有任何意義。

+0

非常感謝你能幫助我很多 – user7925257