gpt4 book ai didi

c# - 列表框复选框未绑定(bind)到其源

转载 作者:行者123 更新时间:2023-12-03 10:42:38 25 4
gpt4 key购买 nike

我需要帮助将我的复选框列表绑定(bind)在弹出框中的列表框中。我的 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>

列表框绑定(bind)到的 View 模型中的代码是
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 中。我没有正确绑定(bind)复选框吗?想法?

最佳答案

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

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

For purposes of resolving backing types, WPF XAML is case sensitive by the same rules that the CLR is case sensitive. Object elements, property elements, and attribute names must all be specified by using the sensitive casing when compared by name to the underlying type in the assembly, or to a member of a type.

关于c# - 列表框复选框未绑定(bind)到其源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49475362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com