2014-06-20 142 views
3

我正在編寫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; 

回答

2

您需要根據所需條件將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; 

不要忘記添加IsFavouriteCUST_CONT

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

希望這有助於。

+0

Vyas_27,謝謝你man..stay祝福! ! –

+0

高興地幫助@NiteshKothari。 – Vyas

+0

Vyas_27,你能告訴我,如何防止chechBox.Checked事件?問題是,當我導航到頁面時,默認CheckBox.Checked事件被觸發。如何解決這個問題? –

1

你必須在使用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}}"/> 
+0

Dhaval Ptel,謝謝你爲我的快速反應,我得到錯誤的ConvertBack方法,也在「」 –