c# - 将 Listbox.ItemContainerStyle 绑定(bind)到当前项目属性

标签 c# wpf listbox

在我的 C# Windows 应用商店应用程序中,我试图将 ListBoxItemVisibility 属性绑定(bind)到存在于每个 Items 中的属性在 ItemsSource 中。

基本上我所拥有的是:

class ExampleClass
{
     bool isVisible;
}

在我的数据上下文中,我有一个 ExampleClass 列表。我试图用 ListBox 做的事情如下:

<ListBox x:Name="ExampleLB" DataContext=""{StaticResource myContext}" ItemsSource="{Binding ExampleClassList}">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <!--This doesnt work-->
            <Setter Property="Visibility" Value="{Binding isVisible, Converter={StaticResource VisibilityConverter}"/>
        </Style>
    </ListBox.ItemContainerStyle>
    /*More code here*/
</ListBox>

但我无法绑定(bind)到 ListBox.ItemContainerStyle 内的 isVisible 属性。相反,它希望我绑定(bind)到 DataContext 的另一个属性。如果我向下移动几行到 ListBox.ItemTemplate,我就可以绑定(bind)到 ItemsSource 中各个 ExampleClass 项目的属性,但为什么我无法在 ItemContainerStyle 内部绑定(bind)到上面几行的那些相同属性?

最佳答案

您不能绑定(bind)到字段。您需要将其设为属性并实现 INotifyPropertyChanged

bool isVisible;
public bool IsVisible
{
  get { return isVisible;}
  set
  {
    isVisible = value;
    OnPropertyChanged("IsVisible");
  }
}

关于c# - 将 Listbox.ItemContainerStyle 绑定(bind)到当前项目属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31276186/

相关文章:

.net - WPF 列表框空数据模板

c# - 控制台应用程序中的 PetaPoco "already an open DataReader"

c# - 如何在 WPF 中创建一个带有绑定(bind)的验证文本框,在无效输入后切换回最后一个有效值?

c# - 在 .NET 中从内存下载并运行 JAR

wpf - 如何使图像绕 z 轴自旋/旋转

c# - 如何在 WPF 应用程序的触摸屏上自然滚动列表框?

c# - Nhibernate - 创建 SQLQuery - IndexOutOfRangeException

wpf - 如何将输入键绑定(bind)到wpf中的itemscontrol

wpf - NAudio低通滤波器

c# - 为什么在 XAML 中添加的 ListBoxItems 没有样式?