2012-10-24 3 views
0

동적으로 체크 박스를 16 번 생성해야하는 WPF에서 작업하고 있습니다.동적 체크 박스 생성 실패

XAML :이 체크 박스의 16 배를 기록하고 개별 버튼이 그들에게 명령을 클릭 한 경우

<Checkboxes Height="14" Command="{Binding CheckboxesGen}" Margin="0" Name="checkBox1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" /> 

위의 방법을 사용하여, 그것은 비효율적 일 수 있습니다. 내 C++ 응용 프로그램에서 같은 상황에 직면했다

private ICommand mCheckboxesGen; 
    public ICommand CheckboxesGen 
    { 
     get 
     { 
      if (mCheckboxesGen== null) 
       mCheckboxesGen= new DelegateCommand(new Action(mCheckboxesGenExecuted), new Func<bool>(mCheckboxesGenCanExecute)); 

      return mCheckboxesGen; 
     } 
     set 
     { 
      mCheckboxesGen= value; 
     } 
    } 

    public bool mCheckboxesGenCanExecute() 
    { 
     return true; 
    } 

    public void mCheckboxesGenExecuted(some INDEX parameter which gives me selected Checkboxes) 
    { 
     // Have a common method here which performs operation on each Checkboxes click based on INDEX which determines which Checkboxes I have selected 
    } 

다음과 같이 내가 이상적으로 그들에게 16 배를 생성하고 내 뷰 모델 클래스에서 하나의 일반적인 방법을 갖고 싶어한다.

for(int j = 0; j < 16; j ++) 
    { 
     m_buttonActiveChannels[j] = new ToggleButton(); 
     addAndMakeVisible(m_buttonActiveChannels[j]); 
     m_buttonActiveChannels[j]->addButtonListener(this); 
    } 

//Checking which Checkboxes is clicked 
unsigned bit = 0x8000; 
for(int i = 15; i >= 0; i--) 
{ 
    if(0 != (value & bit)) //Value has some hardcoded data 
    { 
     m_buttonActiveChannels[i]->setToggleState(true); 
    } 
    else 
    { 
     m_buttonActiveChannels[i]->setToggleState(false); 
    } 

    bit >>= 1; 
} 

는 따라서이 그것을 16 배를 생성하고 index i에 따라 작업을 수행하는 하나의 방법이 있습니다 사용하여 다음과 같이 내가 내 C++ 응용 프로그램에서 일을했다.

비슷한 접근 방식이나 다른 접근 방식을 사용하면 어떻게하면 내 wpf 응용 프로그램에서이를 수행 할 수 있습니까? :) 도와주세요 :)

답변

1

어때?

<ItemsControl ItemsSource="{Binding CollectionOfObjectsThatRepresentYourCheckBox}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <StackPanel Orientation="Horizontal" 
         IsItemsHost="True" /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Checkbox Content="{Binding DisplayText }" Checked="{Binding Checked}" /> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

당신은 부하 또는 명령이 실행되었을 때, 당신은 당신이 그것을 위해 만든 모델에서 확인되는 항목에 대응할 수의 개체에 컬렉션을 채울 필요가있을 것이다 ..

public class CheckBoxClass 
{ 
public int Index {get; set;} 
public string DisplayText {get; set} 
private bool _checked; 
public bool Checked 
{ 
    get { return _checked;} 
    set { 
     _checked = value 
     doSomethingWhenChecked(); 
     } 
} 

ObservableCollection<CheckBoxClass> CollectionOfObjectsThatRepresentYourCheckBox = SomeMethodThatPopulatesIt(); 

이 작업을 수행하는 훨씬 더 깨끗한 방법이며 컨트롤을 생성하는 대신 확인란으로 표시되는 개체 목록에 바인딩하는 것입니다.

+0

그래, 맞아 :)하지만 이것은 하나 이상의 다른 확인란을 생성합니다. 이상적으로 요구 사항 당 하나의 행 자체에 16 개의 chckbx가 있어야합니다. – StonedJesus

+0

ItemsPanelTemplate 사용 http://msdn.microsoft.com/en-us/library/system.windows.controls.itemspaneltemplate.aspx – Dtex

+1

@StonedJesus 괜찮습니다. 업데이트 된 코드 참조 - 원하는 방식으로 표시되어야합니다 :) – Steoates

0

체크 박스에 대한보기 모델을 정의하면이 클래스는 Index 속성과이를 기반으로하는 명령 구현을 갖게됩니다. ObservableCollection of checkboxes 뷰 모델을 현재 뷰 모델에 추가하십시오. 보기에서 적절한 ItemTemplate을 사용하여이 컬렉션에 바인딩 된 ItemsControl을 추가합니다. 이제 뷰 모델에 원하는만큼 많은 체크 박스를 추가 할 수 있습니다.

+0

감사합니다 :) 만약 당신이 샘플 코드와 정교한 pls 감사합니다 :) – StonedJesus