9

私はWPFを始めて2週間しか経っていないので、これはおそらく些細な質問です. バインドしたいいくつかのプロパティを持つコレクション「CellList」があるToolTipため、現在のインスタンスのラベル情報にカーソルを合わせるCellListと表示されます。それ、どうやったら出来るの?私は単純なバインディングを理解しています。これもおそらく単純なバインディングですが、頭を包むことはできません。以下は、ラベルの XAML です。誰かが私にこれを達成する方法を説明してもらえますか?

<HierarchicalDataTemplate>
      <ListBox ItemsSource="{Binding CellList}">
           <ListBox.ItemTemplate>
               <DataTemplate>
                 <Label Content=" " Height="20" Width="15" Background="{Binding Path=ExptNameBkg, Converter={StaticResource ExptNameToBrushConverter}}"                                                   BorderBrush="Black" BorderThickness="1" >
                  </Label>  
              </DataTemplate>                                    
            </ListBox.ItemTemplate>   
       </ListBox>
</HierarchicalDataTemplate>

ありがとう。

4

3 に答える 3

25

ToolTipsについて注意が必要なのは、aToolTipがコントロールに関連付けるオブジェクトであり、コントロールのビジュアル ツリーの一部ではないことです。したがって、ビジュアル ツリーにデータを入力する方法でデータを入力することはできません。たとえば、次のようになります。

<TextBox.ToolTip>
   <StackPanel>
      ...put bound controls here
   </StackPanel>
</TextBox.ToolTip>

代わりに、ToolTip の特定のインスタンスを作成し、それを設定するスタイルを割り当てる必要がありますDataContext(非常に重要です。これが、「配置ターゲット」、つまりコントロールのデータ ソースのプロパティにバインドする方法です)。ツールチップを表示している) とそのTemplate. ToolTip次に、バインディングを含むのビジュアル ツリーをテンプレートに入れます。最後に、ToolTipコントロールで を参照します。

したがって、検証TextBoxを行う人は次のとおりです。Binding

<TextBox ToolTip="{StaticResource ErrorToolTip}">
    <TextBox.Text>
        <Binding Source="SourceProperty">
            <Binding.ValidationRules>
               <DataErrorValidationRule/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

これを使用しますToolTip

<ToolTip x:Key="ErrorToolTip" Style="{StaticResource ErrorToolTipStyle}"/>

そして は、のバインディング ソースのプロパティToolTipからコンテンツを取得するこのスタイルを使用します。ValidationErrorTextBox

<Style x:Key="ErrorToolTipStyle" TargetType="{x:Type ToolTip}">
    <Setter Property="OverridesDefaultStyle" Value="true"/>
    <Setter Property="HasDropShadow" Value="True"/>
    <Setter Property="DataContext" Value="{Binding Path=PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToolTip">
                <Border
                    Name="Border" 
                    BorderThickness="1" 
                    BorderBrush="LightGray">
                    <StackPanel Orientation="Vertical">
                        <Label Background="Firebrick" Foreground="White" FontWeight="Bold" Margin="4">Validation error</Label>
                        <TextBlock Margin="10" Text="{Binding ValidationError}"/>
                    </StackPanel>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="HasDropShadow" Value="true">
                        <Setter TargetName="Border" Property="CornerRadius" Value="4"/>
                        <Setter TargetName="Border" Property="SnapsToDevicePixels" Value="true"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

これについては確信が持てませんが、実際にスタイルに設定する必要がある上記の唯一の部分は、 ;のDataTrigger設定だけだと思います。他のほとんどすべては、のビジュアル ツリーDataContextで明示的に設定できると思います。ToolTipでも、きっと大事なことを考えていない。

于 2010-02-06T19:32:51.303 に答える
20
<Label Content={Binding Path=Id} ToolTip={Binding Path=Name}/>

これを試してみてください

于 2010-02-06T05:54:34.510 に答える
5

これは、単なるテキストよりも少し複雑なツールチップを含む kaxaml 対応の例です。

<Page
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <XmlDataProvider x:Key="CharacterData">
      <x:XData>
        <Data xmlns="">
          <Character First="Bart" Last="Simpson" Background="LightGreen" />
          <Character First="Homer" Last="Simpson" Background="LightBlue" />
          <Character First="Lisa" Last="Simpson" Background="Pink" />
          <Character First="Maggie" Last="Simpson" Background="Yellow" />
          <Character First="Marge" Last="Simpson" Background="PapayaWhip" />
        </Data>
      </x:XData>
    </XmlDataProvider>
    <ToolTip x:Key="ElaborateToolTip">
      <Grid Margin="5">
        <Rectangle RadiusX="6" RadiusY="6" Fill="{Binding XPath=@Background}" />
        <StackPanel Orientation="Horizontal" Margin="10">
          <TextBlock Text="{Binding XPath=@First}" Margin="0,0,6,0" />
          <TextBlock Text="{Binding XPath=@Last}" />
        </StackPanel>
      </Grid>
    </ToolTip>
  </Page.Resources>
  <ListBox ItemsSource="{Binding Source={StaticResource CharacterData}, XPath=Data/Character}">
    <ListBox.ItemContainerStyle>
      <Style TargetType="{x:Type ListBoxItem}">
        <Setter Property="ToolTip" Value="{StaticResource ElaborateToolTip}" />
      </Style>
    </ListBox.ItemContainerStyle>
    <ListBox.ItemTemplate>
      <DataTemplate>
        <TextBlock Text="{Binding XPath=@First}" />
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>
</Page>
于 2010-02-06T19:47:57.207 に答える