2011-01-14 61 views
0

我想从Silverlight滑块读取经过分析的int值以创建复选框。使用C#中的int值创建复选框

例如滑块的值为7,我将按下一个按钮并设置7个复选框。

我该怎么做?

+0

请注意了Windows Phone 7是基于Silverlight 3中(不4)。我相应地更新了您的问题(和标签)。 – 2011-01-14 09:45:32

回答

0

这是一个工作示例。它甚至可以在添加更多内容时记住已检查的框的状态。

假设这XAML:

<Slider Minimum="0" Maximum="7" SmallChange="1" LargeChange="1" 
     x:Name="mySlider" ValueChanged="mySlider_ValueChanged" /> 

<StackPanel x:Name="chkContainer" /> 

这是事件处理程序

private void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) 
{ 
    if (chkContainer != null) // It could be null during page creation (add event handler after construction to avoid this) 
    { 
     // The following works because the both the small and large change are one 
     // If they were larger you may have to add (or remove) more at a time 
     if (chkContainer.Children.Count() < mySlider.Value) 
     { 
      chkContainer.Children.Add(new CheckBox { Content = mySlider.Value.ToString() }); 
     } 
     else 
     { 
      chkContainer.Children.RemoveAt(int.Parse(mySlider.Value.ToString())); 
     } 
    } 
} 
+0

马特规则..:p – 2011-04-12 22:37:53

0

可以使用以下代码实例化复选框并将其添加到默认项目页面。

 var cb = new CheckBox(); 
     ContentPanel.Children.Add(cb); 
1

如果您需要捕获它们的值在视图模型,将复选框中的代码隐藏可能不最好的方法。

class MainWindowViewModel : INotifyPropertyChanged 
{ 
    private int _sliderValue; 
    public int SliderValue 
    { 
     get 
     { 
      return _sliderValue; 
     } 
     set 
     { 
      _sliderValue = value; 

      while (SliderValue > CheckboxValues.Count) 
      { 
       CheckboxValues.Add(false); 
      } 

      // remove bools from the CheckboxValues while SliderValue < CheckboxValues.Count 
      // ... 
     } 
    } 


    private ObservableCollection<Boolean> _checkboxValues = new ObservableCollection<Boolean>(); 
    public ObservableCollection<Boolean> CheckboxValues 
    { 
     get 
     { 
      return _checkboxValues; 
     } 
     set 
     { 
      if (_checkboxValues != value) 
      { 
       _checkboxValues = value; 
       RaisePropertyChanged("CheckboxValues"); 
      } 
     } 
    } 

然后在XAML中,是这样的:

<ItemsControl ItemsSource="{Binding CheckboxValues}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate DataType="{x:Type sys:Boolean}"> 
       <CheckBox IsChecked="{Binding self}">Hello World</CheckBox> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
    </ItemsControl>