3

そのため、正しくバインドできないツリービューのスタイルがあります。スタイルが使用されているユーザー コントロールのグラフの高さプロパティに高さをバインドしました。ただし、不明な理由でユーザー コントロールが見つかりません。誰かがこの問題に光を当ててくれることを望んでいました。テンプレートのプロパティをテンプレート化された親以外のものにバインドすることは禁止されていますか? また、スタイル内にあるという理由だけで要素を見つけることができないのはなぜですか。

xaml ファイルの先頭から:

<UserControl 
  x:Class="WpfExperimental.GraphViewer"
  x:Name="graph_viewer"

そしてスタイル:

   <Style x:Key="SignalNameTreeViewStyle" TargetType="TreeView">
      <Setter Property="OverridesDefaultStyle" Value="True" />
      <Setter Property="SnapsToDevicePixels" Value="True" />
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="TreeView">
            <ScrollViewer x:Name="SignalNameTreeView_ScrollViewer" HorizontalScrollBarVisibility="Disabled" VerticalScrollBarVisibility="Hidden">
              <StackPanel>
                <wpfExp:SignalNameBox x:Name="TreeViewTimeTextBox" Grid.Row="0" Grid.Column="0"
                      Height="{Binding ElementName=graph_viewer, Path=GraphHeight, Mode=OneWay}"
                      Width="200"
                      Margin="19,0,0,0"
                      MainText="Time" 
                    />
                <ItemsPresenter/>
              </StackPanel>         
            </ScrollViewer>
            <ControlTemplate.Triggers>
              <Trigger Property="ItemsControl.HasItems" Value="False">
                <Setter TargetName="TreeViewTimeTextBox"
                        Property="Visibility"
                        Value="Collapsed"/>
              </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>

現在、このバインド試行からデータ バインディング エラーが発生します

ystem.Windows.Data Error: 39 : BindingExpression path error: 'GraphHeight' property not found on 'object' ''UserControl' (Name='graph_viewer')'. BindingExpression:Path=GraphHeight; DataItem='UserControl' (Name='graph_viewer'); target element is 'SignalNameBox' (Name='TreeViewTimeTextBox'); target property is 'Height' (type 'Double')
System.Windows.Data Error: 39 : BindingExpression path error: 'GraphHeight' property not found on 'object' ''UserControl' (Name='graph_viewer')'. BindingExpression:Path=GraphHeight; DataItem='UserControl' (Name='graph_viewer'); target element is 'SignalGraphAxis' (Name='signal_axis'); target property is 'GraphHeight' (type 'Int32')
System.Windows.Data Error: 39 : BindingExpression path error: '_SignalDataViewModel' property not found on 'object' ''UserControl' (Name='graph_viewer')'. BindingExpression:Path=_SignalDataViewModel.MaxTimeValue; DataItem='UserControl' (Name='graph_viewer'); target element is 'SignalGraphAxis' (Name='signal_axis'); target property is 'MaxTimeValue' (type 'Int32')
4

1 に答える 1

2

ElementNameコントロール テンプレートの外部で要素を参照するために使用できないことは確かです。(現在、その効果に関するドキュメントは見つかりません。) できたとしても、実際には意味がありません。隠れた依存関係を含むスタイルを作成しようとしているため、実行時エラーが発生する可能性があります。

別の方法として、依存関係プロパティをコントロールに追加することもできます。を拡張するクラスを作成し、名前付きまたは類似 TreeViewの DP を指定します。SignalNameBoxHeight

public class ExtendedTreeView : TreeView
{
    public double SignalNameBoxHeight
    {
        get { return (double)GetValue(SignalNameBoxHeightProperty ); }
        set { SetValue(SignalNameBoxHeightProperty, value); }
    }

    public static readonly DependencyProperty SignalNameBoxHeightProperty =
        DependencyProperty.Register("SignalNameBoxHeight", 
        typeof(double), 
        typeof(ExtendedTreeView), 
        null);

    public ExtendedTreeView ()
    {
        this.DefaultStyleKey = typeof(Treeview);
    }
}

次にTemplateBinding、コントロール テンプレート内で使用してプロパティを設定できます。

<wpfExp:SignalNameBox
    `Height="{TemplateBinding SignalNameBoxHeight}"`
/>

あとは、スタイルのコンシューマーがバインディング ソースを提供するだけです。

<my:ExtendedTreeView 
    SignalNameBoxHeight="{Binding ElementName=graph_viewer, Path=GraphHeight}" />

編集

コントロール テンプレートの外側の要素FindAncestorを参照するために使用できるようです。

{RelativeSource FindAncestor} は、コントロール テンプレートまたは予測可能な自己完結型 UI コンポジションで主に使用されます。これは、コントロールが常に特定の先祖タイプのビジュアル ツリーにあることが期待される場合に使用されます。

もちろん制限は、これが兄弟ではなく、コントロールの祖先に対してのみ可能であることです。

于 2013-07-12T21:47:38.360 に答える