トップ回答者
DataGrid中に、DataGridで構成されたツールチップを表示したい

質問
-
いつもお世話になっております。
タイトルの件に関して、以下の機能の実現がどうしてもできなかったため、質問させていただきます。
よろしければご教授、よろしくお願いします。
■作成環境
Visual Studio 2017
■作成したい機能
・DataGridの列選択時、DataGridで作成されたデータがツールチップで表示される。
・ツールチップで表示する値はバインドされたデータを表示したい。
■現状
機能実現のためにWPF側で以下のコードを実装。が、C♯側で、【tooltipTable】が認識されないため、itemsSourceが設定できず、tooltipとして空のDataGridが表示される
<Window x:Class="WpfApp1.MainWindow"
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:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="300" Width="300">
<Grid>
<DataGrid x:Name="testTable" Margin="10,8,6,10" AutoGenerateColumns="False" Grid.Row="2" CanUserAddRows="False" >
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="Visibility" Value="{Binding Visible}"/>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTextColumn Header="列1" Width="*" IsReadOnly="True" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="ToolTip">
<Setter.Value>
<DataGrid x:Name="tooltipTable" HorizontalAlignment="Left" Height="150" Margin="7,10,0,0" VerticalAlignment="Top" Width="350" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="ツールチップ表示列" Width="*" Binding="{Binding toolTipValue}" IsReadOnly="True"/>
</DataGrid.Columns>
</DataGrid>
</Setter.Value>
</Setter>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
回答
-
セル毎にTextBlockが作られて、そのTextBlockに対してそれぞれToolTipが生成されるので、名前を付けて識別することはできません。
ToolTipが生成されたときに発生するLoadedイベントなどを捕捉して、そのToolTipの内側にあるDataGridを取り出すことは可能です。<Window x:Class="WpfApp1.MainWindow" 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:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="300" Width="300"> <Grid> <DataGrid x:Name="testTable" Margin="10,8,6,10" AutoGenerateColumns="False" Grid.Row="2" CanUserAddRows="False" > <DataGrid.Resources> <Style TargetType="{x:Type ToolTip}"> <EventSetter Event="Loaded" Handler="tooltip_Loaded" /> <!-- ToolTipが生成されるときにイベントで呼び出されるように --> </Style> </DataGrid.Resources> <!-- 以下省略 -->
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void tooltip_Loaded(object sender, RoutedEventArgs e) { var tip=(ToolTip)sender; DataGrid tooltipTable = tip.Content as DataGrid; } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク hepasoru 2017年8月21日 11:06
すべての返信
-
セル毎にTextBlockが作られて、そのTextBlockに対してそれぞれToolTipが生成されるので、名前を付けて識別することはできません。
ToolTipが生成されたときに発生するLoadedイベントなどを捕捉して、そのToolTipの内側にあるDataGridを取り出すことは可能です。<Window x:Class="WpfApp1.MainWindow" 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:WpfApp1" mc:Ignorable="d" Title="MainWindow" Height="300" Width="300"> <Grid> <DataGrid x:Name="testTable" Margin="10,8,6,10" AutoGenerateColumns="False" Grid.Row="2" CanUserAddRows="False" > <DataGrid.Resources> <Style TargetType="{x:Type ToolTip}"> <EventSetter Event="Loaded" Handler="tooltip_Loaded" /> <!-- ToolTipが生成されるときにイベントで呼び出されるように --> </Style> </DataGrid.Resources> <!-- 以下省略 -->
public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void tooltip_Loaded(object sender, RoutedEventArgs e) { var tip=(ToolTip)sender; DataGrid tooltipTable = tip.Content as DataGrid; } }
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク hepasoru 2017年8月21日 11:06