【问题标题】:listbox checkbox not binding to its source列表框复选框未绑定到其源
【发布时间】:2018-09-03 15:32:34
【问题描述】:

我需要帮助将我的复选框列表绑定到弹出框中的列表框中。我的 XAML 代码如下

<Popup Name="popUser" Placement="Bottom" PlacementTarget="{Binding ElementName=BtnUserFilter}" StaysOpen="False" Height="100" VerticalAlignment="Top">
            <Border Background="White" BorderBrush="Gray" BorderThickness="1,1,1,1">
                <StackPanel Margin="5,5,5,15">
                    <StackPanel Orientation="Horizontal" Margin="0,0,0,20">
                        <Button Margin="0,0,0,0" Name="BtnSelectAll" Click="BtnSelectAll_Click">
                            <Button.Template>
                                <ControlTemplate>
                                    <TextBlock Text="Select All" Foreground="Blue" Cursor="Hand" />
                                </ControlTemplate>
                            </Button.Template>
                        </Button>

                        <Button Margin="10,0,0,0" Name="BtnUnselectAll" Click="BtnUnselectAll_Click">
                            <Button.Template>
                                <ControlTemplate>
                                    <TextBlock Text="Select None" Foreground="Blue" Cursor="Hand" />
                                </ControlTemplate>
                            </Button.Template>
                        </Button>
                    </StackPanel>
                    <ListBox x:Name="lstUser" BorderThickness="0" Height="100" ItemsSource="{Binding Userlists,  RelativeSource={RelativeSource AncestorType=local:MainWindow}}">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding Path=IsChecked, UpdateSourceTrigger=PropertyChanged}"
                                          Content="{Binding Path=UserIL, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                </StackPanel>
            </Border>
        </Popup>

listbox绑定到的viewmodel中的代码是

Dim test = (From c In Users Select New With {Key c.User}).Distinct
    Dim Cnt As Integer
    For Cnt = 0 To test.Count - 1
        UserLists.Add(New UserList(Of String) With {.IsChecked = True, .UserIL = test(Cnt).User})
    Next

代码填充 UserList 变量中的所有用户,但未显示在 ListBox 中。我没有正确绑定复选框吗?想法?

【问题讨论】:

    标签: c# vb.net mvvm data-binding listbox


    【解决方案1】:

    确保 UserLists 是 ObservableCollection,因为常规列表不会将对其内容所做的更改传播到 UI。

    此外,我怀疑您的 xaml 中有错字,将 UserLists 称为 Userlists(注意区分大小写)

    为了解析支持类型,WPF XAML 区分大小写 与 CLR 区分大小写的规则相同。对象元素, 属性元素和属性名称都必须通过使用指定 按名称与基础类型比较时的敏感大小写 程序集,或类型的成员。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-31
      • 1970-01-01
      • 1970-01-01
      • 2014-08-25
      • 2015-04-04
      • 2016-08-24
      • 1970-01-01
      相关资源
      最近更新 更多