gpt4 book ai didi

c# - 如何设置ListBox中的CheckBox默认选中

转载 作者:行者123 更新时间:2023-11-30 16:11:29 27 4
gpt4 key购买 nike

我正在编写 Windows Phone 8 应用程序。我有一个与类绑定(bind)的 ListBox,它包含 XML 数据。在我的类(class)中,有一个名为 Favorite 的字段,我希望如果 Favorite 等于 0,则应取消选中 CheckBox,如果等于 1,则应选中 CheckBox。有关详细信息,请参阅下面的代码:

<ListBox x:Name="listBox1" Width="429" Height="621" HorizontalAlignment="Left" 
Margin="21,43,0,59" VerticalAlignment="Top" ItemsSource="{Binding}"
SelectedItem="{Binding}" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="440">
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Margin="5" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Margin="5" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
<StackPanel>
<CheckBox x:Name="CheckBox1" IsChecked="False" Height="72" Foreground="Black" Margin="358,-110,22,0" BorderBrush="Black" Loaded="CheckBox1_Loaded" Checked="CheckBox1_Checked" Unchecked="CheckBox1_Unchecked" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

这是我的代码隐藏文件:

XDocument doc = XDocument.Parse(e.Result);
List<CUST_CONT> customers = new List<CUST_CONT>();

customers = (from query in doc.Descendants("row")
select new CUST_CONT
{
Id = query.Element("Id").Value,
Name = query.Element("Name").Value,
Address = query.Element("Address").Value,
Favourite = (query.Element("Favourite").Value)
}).ToList();
listBox1.DataContext = customers;

最佳答案

您需要根据需要的条件对 CheckBox 进行数据绑定(bind)。在这里,尝试实现这个;

<ListBox x:Name="listBox1" Width="429" Height="621" HorizontalAlignment="Left" Margin="21,43,0,59" VerticalAlignment="Top" ItemsSource="{Binding}" SelectedItem="{Binding}" SelectionMode="Extended">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="440">
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Margin="5" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>
<TextBlock Text="{Binding}" TextWrapping="Wrap" Foreground="Black" FontSize="22" Margin="5" Height="30" TextAlignment="Left" Width="Auto" FontWeight="SemiBold"/>

<StackPanel>
<CheckBox x:Name="CheckBox1" IsChecked="{Binding IsFavourite}" Height="72" Foreground="Black" Margin="358,-110,22,0" BorderBrush="Black" Loaded="CheckBox1_Loaded" Checked="CheckBox1_Checked" Unchecked="CheckBox1_Unchecked" />
</StackPanel>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

然后在你的代码中;

XDocument doc = XDocument.Parse(e.Result);
List<CUST_CONT> customers = new List<CUST_CONT>();

customers = (from query in doc.Descendants("row")
select new CUST_CONT
{
Id = query.Element("Id").Value,
Name = query.Element("Name").Value,
Address = query.Element("Address").Value,
Favourite = (query.Element("Favourite").Value)
}).ToList();

for (int i = 0; i < customers.Count; i++)
{
if (customers.ElementAt(i).Favourite == "0")
{
customers.ElementAt(i).IsFavourite = "False";
}
else
{
customers.ElementAt(i).IsFavourite = "True";
}
}

listBox1.DataContext = customers;

不要忘记在 CUST_CONT 类中添加 IsFavourite

public class CUST_CONT
{
public string IsFavourite { get; set; }
}

希望这对您有所帮助。

关于c# - 如何设置ListBox中的CheckBox默认选中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24321380/

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