【问题标题】:How to generate Togglebutton Dynamically and Perform Operation on Each based on Index in WPF如何在WPF中基于索引动态生成Togglebutton并对其执行操作
【发布时间】:2012-10-14 10:11:44
【问题描述】:

我正在开发一个 WPF,我需要在其中动态生成 16 次 Togglebutton。如果我写下这个切换按钮 16 次并为它们设置单独的按钮单击命令,那将是低效的。

XAML:

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

理想情况下,我希望生成它们 16 次,并在我的视图模型类中使用一种通用方法,如下所示:

private ICommand mTogglebuttonGen;
    public ICommand TogglebuttonGen
    {
        get
        {
            if (mTogglebuttonGen == null)
                mTogglebuttonGen = new DelegateCommand(new Action(mTogglebuttonGenExecuted), new Func<bool>(mTogglebuttonGenCanExecute));

            return mTogglebuttonGen;
        }
        set
        {
            mTogglebuttonGen = value;
        }
    }

    public bool mTogglebuttonGenCanExecute()
    {
        return true;
    }

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

我在我的 C++ 应用程序中完成了如下操作:

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

//Checking which Togglebutton 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 执行操作。

如何在我的 wpf 应用程序中实现它? :)

【问题讨论】:

    标签: c# .net wpf mvvm checkbox


    【解决方案1】:

    在您的视图模型中,创建一个包含 16 个 ToggleAction 类实例的集合。

    public ObservableCollection<MyToggleActionClass> MyItems {get;set;}
    
    
    public class MyToggleActionClass
    {
       public string DisplayName {get;set;}
       public int Index {get;set;}
    }
    

    将此集合绑定到 ItemsControl 的 ItemsSource,就是这样。

    <ItemsControl ItemsSource="{Binding MyItems}">
     <ItemsControl.ItemTemplate>
      <DataTemplate>
        <ToggleButton Content="{Binding Displayname}"
                      Command="{Binding DataContext.TogglebuttonGen, RelativSource={RelativSource AncestorType=ItemsControl}}"
                      CommandParameter="{Binding Index}"/>
      </DataTemplate>
     </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    这都是手写的,所以请检查是否有错误。最重要的是命令绑定。如果你想从你的 ViewModel 调用命令,你必须使用 RelativeSource 走到 ViewModel DataContext。

    我还将在 MyToggleActionClass 中处理/绑定切换状态。

    【讨论】:

    • 感谢您的回复。我在集合中创建了 16 个项目,并且还按照您的建议尝试了绑定,但在命令中抛出错误 'ItemsControl' member is not valid because it does not have a qualifying type name。这里有什么问题? :)
    • 我认为我的 RelativeSource 绑定不正确,我编辑了示例。但我没有 IDE 来检查我的语法 atm。
    • 是的,现在工作正常 :) 虽然我需要正确对齐 :)
    • 我希望按钮排成一行。这就像一个堆栈面板。按钮是一个在另一个之上。'
    • 将 ItemsControl.ItemsPanel 设置为您想要的布局面板
    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2021-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多