2011-01-14 10 views
0

silverlightスライダから解析されたint値を読み取ってチェックボックスを作成したい。C#のint値からチェックボックスを作る(銀色の場合)

たとえば、スライダの値が7の場合、ボタンを押して7つのチェックボックスを作成します。

どうすればよいですか?

+0

のWindows Phone 7は、Silverlight 3の(ない4)に基づいていますのでご注意ください。あなたの質問(およびタグ)を適宜更新しました。 –

答えて

0

ここでは動作例を示します。さらに多くが追加されると、チェックボックスの状態を記憶することさえできます。

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

<StackPanel x:Name="chkContainer" /> 

これは、あなたがのviewmodelにその値をキャプチャする必要がある場合は、コードビハインドにチェックボックスを追加することができない場合があり、イベントハンドラ

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 –

0

次のコードを使用して、チェックボックスをインスタンス化してデフォルトプロジェクトページに追加することができます。

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

です:このXAMLを想定し

最良のアプローチ。 XAMLで、その後

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

、のようなもの:

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