2017-06-05 10 views
0

ラベルとテキストボックスを持つユーザーコントロールを作成しました。 2つのDependencyProperties(TextとLabel)を追加し、それらを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依存関係のプロパティは、ビューモデルではなくコントロールで定義されています。 Label

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

への結合と TextBox

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

への結合に変更 BindingModeUpdateSourceTriggerプロパティをお読みください。彼らはどのように動作するのかわかりません。 Mode=TwoWay, UpdateSourceTrigger=PropertyChangedContentプロパティの意味を持ちません。

+0

ありがとうございました – user7925257

関連する問題