[Silverlight] ListBoxの各ItemでToolTip表示したい。
13777 ワード
ListBoxの各Itemにラジオボタンを表示しようとする場合、
IsHitTestVisible="True"とすると挙動がおかしくなる。
ただし、IsHitTestVisible="False"にするとToolTipがうまく表示されなくて悩みました。
下記のようなコードで解決しました。
Person.cs
using System;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication5
{
public class Person : INotifyPropertyChanged
{
public string Name
{
set;
get;
}
private bool _isSelected = false;
public bool IsSelected
{
set
{
_isSelected = value;
OnPropertyChanged("IsSelected");
}
get { return _isSelected; }
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
MainPage.xaml
<UserControl x:Class="SilverlightApplication5.MainPage"
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:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<ListBox x:Name="personList"
SelectionChanged="personList_SelectionChanged"
>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<toolkit:WrapPanel
Width="300"
></toolkit:WrapPanel>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<RadioButton Content="{Binding Name}"
Checked="RadioButton_Checked"
IsHitTestVisible="False"
IsChecked="{Binding IsSelected, Mode=TwoWay}"
>
</RadioButton>
</DataTemplate>
</ListBox.ItemTemplate>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="ToolTipService.ToolTip" Value="{Binding Name}"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
</Grid>
</UserControl>
MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace SilverlightApplication5
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
ObservableCollection<Person> persons = new ObservableCollection<Person>
{
new Person { Name = "Test1"},
new Person { Name = "Test2"},
new Person { Name = "Test3"},
new Person { Name = "Test4"},
new Person { Name = "Test5"},
new Person { Name = "Test6"},
new Person { Name = "Test7"},
new Person { Name = "Test8"},
new Person { Name = "Test9"},
new Person { Name = "Test10"},
};
this.personList.ItemsSource = persons;
}
private void RadioButton_Checked(object sender, RoutedEventArgs e)
{
}
private void personList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
(e.AddedItems[0] as Person).IsSelected = true;
}
}
}
Author And Source
この問題について([Silverlight] ListBoxの各ItemでToolTip表示したい。), 我々は、より多くの情報をここで見つけました https://qiita.com/hidetzu/items/1aefd9d337a619e2e0c1著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .