0

チェックボックスを16回動的に生成する必要があるWPFに取り組んでいます。

XAML:

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

上記の方法を使用すると、このチェックボックスを16回書き留めて、個別のボタンクリックコマンドを使用すると非効率になります。理想的には、それらを16回生成し、viewmodelクラスに次のような1つの一般的なメソッドを設定します。

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
    }

私はC++アプリでも同じ状況に直面していました。私はC++アプリで次のようにそれを行いました:

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回生成され、に基づいて操作を実行する1つのメソッドがありますindex i

同様のアプローチまたは他のアプローチを使用して、wpfアプリでそれをどのように達成できますか?:) 助けてください :)

4

2 に答える 2

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();

これはこれを行うためのよりクリーンな方法であり、コントロールを生成する代わりに、チェック ボックスで表されるオブジェクトのリストにバインドするだけです。

于 2012-10-24T13:55:31.817 に答える
0

チェックボックスのビューモデルを定義します。このクラスには、Index プロパティとそれに基づくコマンド実装があります。チェックボックス ビューモデルの ObservableCollection を現在のビューモデルに追加します。ビューで、適切な ItemTemplate を使用して、このコレクションにバインドされた ItemsControl を追加します。ビューモデルに必要な数のチェックボックスを動的に追加できるようになりました。

于 2012-10-24T13:44:46.440 に答える