【问题标题】:How to set CheckBox in ListBox checked by default如何在 ListBox 中设置 CheckBox 默认选中
【发布时间】:2014-06-20 15:01:28
【问题描述】:

我正在编写一个 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;

【问题讨论】:

    标签: c# windows-phone-8 checkbox


    【解决方案1】:

    您需要根据您想要的条件对 CheckBox 进行 DataBind。在这里,尝试实现这个;

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

    希望这会有所帮助。

    【讨论】:

    • 乐于帮助@NiteshKothari。
    • Vyas_27,你能告诉我,如何防止 chechBox.Checked 事件吗?问题是当我导航到页面时,默认情况下会触发 CheckBox.Checked 事件。如何解决这个问题?
    • @NiteshKothari 尝试在绑定列表框后添加您的 eventHandler。即从您的 XAML 中删除 Checked="CheckBox1_Checked" Unchecked="CheckBox1_Unchecked" 并添加此 CheckBox1.Checked += Your Event Handler & CheckBox1.Unchecked += Aother Event Handler。 listBox1.DataContext=customers 之后的CODE。看看这是否有帮助。
    • Vyas_27,但是我的复选框在 listBox 里面,所以我无法像“CheckBox1.Checked”一样访问它。我该如何访问它?非常感谢!!
    • 在页面开头添加;私人布尔 isBinding { 得到;放; } 然后 isBinding = true; listBox1.DataContext = 客户; isBinding = 假;并且您的 Checked & Unchecked 事件处理程序中的代码将其添加到 if (!isBinding){ "ADD YOUR CODE HERE" } 之间。当您绑定列表框时,选中和未选中事件将基于绑定触发,但不会执行任何代码,因为 isBinding 为真。看看这是否有帮助。
    【解决方案2】:

    你必须使用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}}"/>
    

    【讨论】:

    • Dhaval Ptel,感谢您的快速响应,我在 ConvertBack 方法以及“”中遇到错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    相关资源
    最近更新 更多