如何将“列表框”中的“复选框”设置为默认选中

本文关键字:复选框 设置 默认 中的 列表 列表框 | 更新日期: 2023-09-27 18:09:33

我正在写一个Windows Phone 8应用程序。我有一个绑定了类的ListBox,它包含XML数据。在我的类中有一个名为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。在这里,尝试实现这个;

<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; }
}

你必须使用IValueConverter

 public class YesNoToBooleanConverter : IValueConverter
    {
            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                    if(value.ToString()=="0")
                      {
                         return false;
                      }
                    else
                     {
                        return true;
                     }
            }
            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
            }
    }
XAML 中的

<Window.Resources>
            <local:YesNoToBooleanConverter x:Key="YesNoToBooleanConverter" />
</Window.Resources>

<CheckBox IsChecked="{Binding Favourite,Mode="TwoWay", Converter={StaticResource YesNoToBooleanConverter}}"/>