2010-02-15 15 views
2

我有一個列表框。我有一個項目模板和一個堆棧面板。 它在項目模板中有一個文本框和一個複選框。我們如何訪問Itemtemplate中的控件Silverlight

有沒有一種方法,我可以訪問該複選框,啓用/禁用所選指標變化?

<UserControl.Resources> <DataTemplate x:Key="UserApplicationsTemplate"> <StackPanel Orientation="Horizontal" Margin="2" ToolTipService.Placement="Mouse" ToolTipService.ToolTip="{Binding Description, Mode=OneWay}"> <TextBlock Text="{Binding Mode=OneWay}" TextWrapping="Wrap" Width="100" DataContext="{Binding ApplicationName, Mode=OneWay}" /> <CheckBox x:Name="{Binding ApplicationName, Mode=OneWay}" Margin="5,0,0,0" Click="IsActive_Clicked" IsChecked="{Binding IsActive, Mode=OneWay}" Content="IsActive"/> </StackPanel> </DataTemplate> </UserControl.Resources>

<ListBox x:Name="lstbxUserApplications" Height="357" ItemsSource="{Binding Mode=OneWay}" SelectionMode="Single" ItemTemplate="{StaticResource UserApplicationsTemplate}" />

回答

0

下面是使用單選按鈕解決方案:
http://leeontech.wordpress.com/2009/03/18/creating-radiobuttonlist/

它應該很容易將其更改爲複選框。

+0

我想在後面的代碼中訪問listboxitem的容器。 我有一個項目模板列表框。項目模板有一個堆棧面板,並且它有一個文本框和一個複選框。 我試圖訪問後面的代碼中的某些邏輯複選框,並禁用它。 我希望可以訪問控制爲我能夠做到,chkbox.IsEnabled =假 – gkbinary

2

Assming你有你的ItemsSource綁定:

<ListBox x:Name="myList"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <CheckBox IsChecked="{Binding Check, Mode=TwoWay}" /> 
       <TextBlock Text="{Binding Name, Mode=TwoWay}" 
          Width="100" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

<Button x:Name="button1" 
     Content="Uncheck 2" 
     Click="button1_Click" /> 

你實際上並不需要改變CheckBox.IsChecked財產,但對你的ItemsSource值:

public partial class MainPage : UserControl 
    { 
     public MainPage() 
     { 
      InitializeComponent(); 
      this.Loaded += new RoutedEventHandler(MainPage_Loaded); 
     } 

     void MainPage_Loaded(object sender, RoutedEventArgs e) 
     { 
      myList.ItemsSource = ListInfo.getInfo(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      ListInfo item = myList.Items[1] as ListInfo; 
      item.Check = !item.Check; 
     } 
    } 

    public class ListInfo : INotifyPropertyChanged 
    { 
     private string name; 
     public string Name 
     { 
      get 
      { 
       return name; 
      } 
      set 
      { 
       name = value; 
       NotifyPropertyChange("Name"); 
      } 
     } 

     private bool check; 
     public bool Check 
     { 
      get 
      { 
       return check; 
      } 
      set 
      { 
       check = value; 
       NotifyPropertyChange("Check"); 
      } 
     } 

     public static ObservableCollection<ListInfo> getInfo() 
     { 
      ObservableCollection<ListInfo> data = new ObservableCollection<ListInfo>(); 
      data.Add(new ListInfo { Name = "Some text 1", Check = true }); 
      data.Add(new ListInfo { Name = "Some text 2", Check = false }); 
      data.Add(new ListInfo { Name = "Some text 3", Check = true }); 

      return data; 
     } 

     public void NotifyPropertyChange(string propertyName) 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 

     #region INotifyPropertyChanged Members 

     public event PropertyChangedEventHandler PropertyChanged; 

     #endregion 
} 

如果坐查看按鈕上Click事件處理程序,可以看到我所做的只是獲取項目並更改了值。這立即反映在用戶界面上。

更新:我看,這是你問的不是什麼。這裏有幾個想法:

你可以有一個事件處理程序的複選框:

<ListBox x:Name="myList"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <CheckBox IsChecked="{Binding Check, Mode=TwoWay}" 
          IsEnabled="True" 
          Content="{Binding Name, Mode=TwoWay}" 
          Click="CheckBox_Click" /> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

,並獲得該代碼後面的參考資料:

private void CheckBox_Click(object sender, RoutedEventArgs e) 
{ 
    CheckBox chk = sender as CheckBox; 
    chk.IsEnabled = false; 
} 

當然,這裏的問題如果您禁用複選框,您將無法訪問Click envent。

另一種選擇是使用VisualTreeHelper獲得關於你的CheckBox當在ListBox中的選擇變化:

private void myList_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ListBox list = sender as ListBox; 
    ListInfo current = list.SelectedItem as ListInfo; 

    List<CheckBox> checkBoxes = new List<CheckBox>(); 
    getCheckBoxes(list, ref checkBoxes); 

    foreach (CheckBox checkBox in checkBoxes) 
    { 
     if (checkBox.Content.ToString() == current.Name) 
     { 
      checkBox.Foreground = new SolidColorBrush(Colors.Red); 
     } 
    } 
} 

public void getCheckBoxes(UIElement parent, ref List<CheckBox> items) 
{ 
    int count = VisualTreeHelper.GetChildrenCount(parent); 
    if (count > 0) 
    { 
     for (int i = 0; i < count; i++) 
     { 
      UIElement child = VisualTreeHelper.GetChild(parent, i) as UIElement; 
      if (child.GetType() == typeof(CheckBox)) 
      { 
       items.Add(child as CheckBox); 
      } 
      getCheckBoxes(child, ref items); 
     } 
    } 
} 

這當然不是性能最好的選擇,但你獲得更多的靈活性。

+0

我試圖訪問UI屬性啓用/禁用該複選框。我沒有問題設置複選框的值。 就像如果我想更改listboxitem的容器的背景顏色或其他屬性,我希望能夠在後面的代碼中執行此操作。 – gkbinary

+0

對不起!我更新了答案,我希望實際上有幫助,如果沒有,請告訴我。 –

+0

我試過用這個 UIElement child = VisualTreeHelper。GetChild(parent,i)作爲UIElement; 但它不給我的孩子,是因爲我使用列表框項目itemtemplate靜態資源? 它並沒有給我足夠的空間來放置XAML。我將嘗試使用XAML更新原始帖子 – gkbinary

相關問題