2017-01-04 10 views
0

ListBoxDisplayMemberPathプロパティを使用していくつかの情報を示すカスタムListBoxItemTemplateListBoxがあります。contentpresenterのツールチップを表示する値に設定するにはどうすればよいですか?

ListBoxItemTemplateは、ContentPresenterです。

ContentPresenterのツールチッププロパティをContentPresenterと同じに設定するだけです。

私はこれを行うにしようとしました:

<ContenPresenter Tooltip={Path Content, RelativeSource={RelativeSource Self}}/> 

しかし、私はDisplayMemberPathロジック(全体のDataContextオブジェクト)なしのコンテキストを取得します。

「DisplayMemberPath」を適用してContentPresenterで表示される値を取得するにはどうすればよいですか?

ありがとうございます。

EDIT:

ここツールチップ(コントロールがバインドして、このスタイルとDisplayMemberPathプロパティを設定している)なしスタイル:

<Style x:Key="CheckListBoxStyle" TargetType="{x:Type ListBox}" > 
    <Setter Property="SelectionMode" Value="Multiple" /> 
    <Setter Property="ItemContainerStyle" Value="{StaticResource CheckListBoxItemStyle}"/> 
    <Setter Property="Width" Value="177"/> 
    <Setter Property="HorizontalAlignment" Value="Left" /> 
    <Setter Property="Height" Value="70"/> 
</Style> 

<Style x:Key="CheckListBoxItemStyle" TargetType="{x:Type ListBoxItem}"> 
    <Setter Property="Background" Value="Transparent" /> 
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
    <Setter Property="Padding" Value="2,0,0,0" /> 
    <Setter Property="Template"> 

     <Setter.Value> 
      <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
       <Border x:Name="Bd" 
          Background="{TemplateBinding Background}" 
          BorderBrush="{TemplateBinding BorderBrush}" 
          BorderThickness="{TemplateBinding BorderThickness}" 
          Padding="{TemplateBinding Padding}" 
          SnapsToDevicePixels="true"> 
        <CheckBox HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
            VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
            IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Tag="CheckBox1"> 
         <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
        </CheckBox> 
       </Border> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

が、私は同時にリストボックスの両方にDisplayMemberPathプロパティとItemTemplateにを使用することが可能であるとは思わない:あなたは、例えば、コンバータを使用することができます。コードを表示してください。 –

+0

@DTsawant:done。 –

答えて

1

「DisplayMemberPath」を適用してContentPresenterで表示される値を取得するにはどうすればよいですか?

純粋なXAMLではこれを動的に行うことはできません。あなたはDisplayMemberPathプロパティの値を知っている場合は、直接このプロパティにバインドすることができことができます。

<CheckBox HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                  IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Tag="CheckBox1" 
                  ToolTip="{Binding Path=Content.Name, ElementName=cp}"> 
    <ContentPresenter x:Name="cp" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
</CheckBox> 

しかし、あなたはToolTip="{Binding Path=Content.[DisplayMemberPath], ElementName=cp}"ような何かをすることはできません。

これを達成するにはコードを書く必要があります。

public class ContentConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     string path = values[0] as string; 
     object dataObject = values[1]; 

     if(!string.IsNullOrEmpty(path) && dataObject != null) 
      return dataObject.GetType().GetProperty(path).GetValue(dataObject).ToString(); 

     return null; 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

<ListBox x:Name="lb11" DisplayMemberPath="Name"> 
    <ListBox.Resources> 
     <local:ContentConverter x:Key="conv" /> 
    </ListBox.Resources> 
    <ListBox.Style> 
     <Style TargetType="{x:Type ListBox}" > 
      <Setter Property="SelectionMode" Value="Multiple" /> 
      <Setter Property="ItemContainerStyle"> 
       <Setter.Value> 
        <Style TargetType="{x:Type ListBoxItem}"> 
         <Setter Property="Background" Value="Transparent" /> 
         <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
         <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
         <Setter Property="Padding" Value="2,0,0,0" /> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
            <Border x:Name="Bd" Background="{TemplateBinding Background}" 
                BorderBrush="{TemplateBinding BorderBrush}" 
                BorderThickness="{TemplateBinding BorderThickness}" 
                Padding="{TemplateBinding Padding}" 
                SnapsToDevicePixels="true"> 
             <CheckBox HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                  VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                  IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}}" Tag="CheckBox1"> 
              <CheckBox.ToolTip> 
               <MultiBinding Converter="{StaticResource conv}"> 
                <Binding Path="DisplayMemberPath" RelativeSource="{RelativeSource AncestorType=ListBox}"/> 
                <Binding Path="Content" ElementName="cp"/> 
               </MultiBinding> 
              </CheckBox.ToolTip> 
              <ContentPresenter x:Name="cp" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" /> 
             </CheckBox> 
            </Border> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </Setter.Value> 
      </Setter> 
      <Setter Property="Width" Value="177"/> 
      <Setter Property="HorizontalAlignment" Value="Left" /> 
      <Setter Property="Height" Value="70"/> 
     </Style> 
    </ListBox.Style> 
</ListBox> 
+0

ありがとう、ありがとう。あなたの言ってる事がわかります。私はdatacontextオブジェクト(コンテンツ)でコンバーターを使用するのが好きではないので、あなたはそれ以上の視覚変換をしているように見えますので、添付プロパティを作成しようとします。とにかくありがとう! –

+0

私は、リフレクションを使用して、データオブジェクトから動的名を持つプロパティの値を取得しています。 DisplayMemberPathの*文字列*値を使用して実際の値を取得する場合は、これを避けることはできません。 – mm8

+0

いいえ、それは問題ありません。それはより多くの懸念の問題です。私の主張は、コンバータは視覚化ロジックのみを持ち、コンバータにDatacontextオブジェクトを渡し、VMロジックを実行する必要があるということです。 –

0

あなたはテンプレート内ContentPresenterを定義しているので、 、あなたはバインディングのコンテンツにアクセスすることはできません。それが働いていたdidntのlevel.so DataContext分布に従って、あなたはコンテンツに既にしている、私は以下のようにContentControlToolTipContentPresenterを変更する

<ContentControl Tooltip={Binding}/> 

に動作します推測私が間違っているなら、私を修正してください。

+0

ControlTemplate内のContentControlを追加しても機能しません。テキストもツールも表示されます:\ –

関連する問題