2017-05-22 10 views
1

私は以下のリソースを持っています。リソース内のテキストブロックに値をバインド

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:local="clr-namespace:samplePrjkt" 
        > 

    <ToolBar x:Key="MyToolbar" Height="120"> 
     <!--Template--> 
     <GroupBox Header="Template" Style="{StaticResource ToolbarGroup}" Margin="3"> 
      <StackPanel Grid.Row="1" Orientation="Horizontal"> 
       <StackPanel Orientation="Vertical" Margin="0,2,0,2"> 
        <TextBlock Text="{Binding TextValue}"></TextBlock> 
       </StackPanel> 
      </StackPanel> 
     </GroupBox> 
    </ToolBar> 
</ResourceDictionary> 

このソースコードは、次のようにWPFユーザーコントロールで使用されます。

<UserControl x:Class="Sampleprjkt.sample.sampleWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
     xmlns:local="clr-namespace:Sampleprjkt"    
     > 
    <Grid Margin="10"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="29*"/> 
      <RowDefinition Height="107*"/> 
     </Grid.RowDefinitions> 

     <ContentControl Content="{StaticResource MyToolbar}"/> 

    </Grid> 
</UserControl> 

私は

public partial class SampleWindow : UserControl 
    { 

     private string _textValue; 

     public string TextValue 
     { 
      get { return _textValue; } 
      set 
      { 
       _textValue = value; 

      } 
     } 

     public SampleWindow() 
     { 
      InitializeComponent(); 
      _textValue = "XXXXX"; 


     } 

    } 

を次のようにWPFユーザーコントロールのコンストラクタ内でこのテキストブロックに値をバインドしようとしているが、私はこれを実行したら、私はに設定されていない「XXXXX」の値を見ることができます<TextBlock Text="{Binding TextValue}"></TextBlock>、私がここで逃したものは?

+0

私はあなたが修正することができたとしrelativeSource: 'Text =" {Binding TextValue、RelativeSource = {RelativeSource AncestorType = SampleWindow}} "を追加することによってバインドします。 – ASh

+0

@ASh - 私はデザインが疑わしいと思うことに同意しますが、ここでRelativeSourceバインディングを追加することで問題をさらに複雑にすることはありませんか?親が常に独立したResourceDictionaryで定義されているコントロールインスタンスに対するUserControl/SampleWindowであることを前提としていますか? – Ada

答えて

0

ContentControlには、nullのDataContextがありません。 Bindingは常にDataContext内のオブジェクトを参照するため、BindingはTextValueを検出しません。

あなたは、単にそれ自体にユーザーコントロールのDataContextのを設定できます

public SampleWindow() 
{ 
    InitializeComponent(); 
    _textValue = "XXXXX"; 

    this.DataContext = this; 
} 

のDataContextは、現在のテキストを表示するのTextBlock、まで継承されます。

0

XAMLで定義したバインディング(あなたの場合はTextValue)のパスは、要素の現在のDataContext(あなたの場合はTextBlock)またはバインディングのソースのプロパティの名前を参照します。

これは、あなたが@Sharada Gururajにより示唆されるようDataContextを設定する必要がありますいずれかのことを意味します

public SampleWindow() 
{ 
    InitializeComponent(); 
    _textValue = "XXXXX"; 
    DataContext = this; 
} 

を...または結合の明示的なソースを指定:

<TextBlock Text="{Binding Path=TextValue, Source={RelativeSource AncestorType=UserControl}}"></TextBlock> 
関連する問題