ListBox "IsSelected" 绑定不起作用
ListBox "IsSelected" binding not working
我到处搜索这个问题的答案,但没有成功,据我所知,我已经准备好了所有正确的元素,但由于某种原因,这不起作用。
当在列表框中选择一个项目时,我需要 IsSelected setter 到 运行。
XAML代码
<ListBox HorizontalAlignment="Left" Height="226" Margin="10,34,0,0" VerticalAlignment="Top" Width="132" ItemsSource="{Binding Activities}" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
C# 视图模型
private bool m_isSelected;
public bool IsSelected
{
get { return m_isSelected; }
set
{
if (value != m_isSelected)
{
m_isSelected = value;
if (m_isSelected)
{
CurrentViews = getCurrentViews((BasePropertyTypeVM)CurrentActivity);
AvailableViews = getAvailableViews(Properties);
}
OnPropertyChanged(() => IsSelected);
}
}
}
非常感谢,
山姆
ListBoxItem的IsSelected
属性绑定到IsSelected
视图模型属性,必须是属性的数据项类型,即 Activities
集合的元素类型。
您需要声明这样的数据项class,例如
public class Activity : INotifyPropertyChanged
{
public string Name { get; set; }
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
if (value != isSelected)
{
value = isSelected;
...
}
}
}
并像这样声明 Activities
集合:
public ObservableCollection<Activity> { get; set; }
Activity class 的 Name
属性 可以用在 ListBoxItem 的数据模板中:
<ListBox ... ItemsSource="{Binding Activites}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
试试这个:
<Setter Property="IsSelected" Value="{Binding DataContext.IsSelected,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}" />
我到处搜索这个问题的答案,但没有成功,据我所知,我已经准备好了所有正确的元素,但由于某种原因,这不起作用。
当在列表框中选择一个项目时,我需要 IsSelected setter 到 运行。
XAML代码
<ListBox HorizontalAlignment="Left" Height="226" Margin="10,34,0,0" VerticalAlignment="Top" Width="132" ItemsSource="{Binding Activities}" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
C# 视图模型
private bool m_isSelected;
public bool IsSelected
{
get { return m_isSelected; }
set
{
if (value != m_isSelected)
{
m_isSelected = value;
if (m_isSelected)
{
CurrentViews = getCurrentViews((BasePropertyTypeVM)CurrentActivity);
AvailableViews = getAvailableViews(Properties);
}
OnPropertyChanged(() => IsSelected);
}
}
}
非常感谢,
山姆
ListBoxItem的IsSelected
属性绑定到IsSelected
视图模型属性,必须是属性的数据项类型,即 Activities
集合的元素类型。
您需要声明这样的数据项class,例如
public class Activity : INotifyPropertyChanged
{
public string Name { get; set; }
private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set
{
if (value != isSelected)
{
value = isSelected;
...
}
}
}
并像这样声明 Activities
集合:
public ObservableCollection<Activity> { get; set; }
Activity class 的 Name
属性 可以用在 ListBoxItem 的数据模板中:
<ListBox ... ItemsSource="{Binding Activites}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsSelected" Value="{Binding IsSelected}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
试试这个:
<Setter Property="IsSelected" Value="{Binding DataContext.IsSelected,
RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBox}}}" />