0

我是一名 C++ 开发人员,最近转向 C#。我在我的 wpf 应用程序中使用 MVVM 模式。我正在研究单选按钮的动态生成。好吧,这个要求很简单,我需要生成 24 个单选按钮,这样一次只检查一个单选按钮。这是代码:

XAML:

<Grid Grid.Row="1">
        <GroupBox Header="Daughter Cards" >
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />                        
                    <ColumnDefinition Width="220" />
                </Grid.ColumnDefinitions>

                <Grid Grid.Column="0">
                    <RadioButton Content="{Binding SlotButtons}" Name="SLotButtons" />
                </Grid> 
            </Grid>
         </GroupBox>
</Grid>

Grid.Column="0"如上所述,我想生成 24 个单选按钮。

视图模型:

// Description of SlotButtons
    private string _SlotButtons;
    public string SlotButtons
    {
        get
        {
            return _SlotButtons;
        }

        set
        {
            _SlotButtons = value;
            OnPropertyChanged("SlotButtons");
        }
    }

//For RadioButton Click
private ICommand mSlotCommand;
    public ICommand SlotCommand
    {
        get
        {
            if (mSlotCommand == null)
                mSlotCommand = new DelegateCommand(new Action(mSlotCommandExecuted), new Func<bool>(mSlotCommandCanExecute));

            return mSlotCommand;
        }
        set
        {
            mSlotCommand = value;
        }
    }

    public bool mSlotCommandCanExecute()
    {
        return true;
    }

    public void mSlotCommandExecuted()
    {
       // Logic to implement on a specific radiobutton click using Index
    }

我在我的 C++ 应用程序中这样做了,如下所示:

for(slot = 0; slot < 24; slot++)
{
    m_slotButton[slot] = new ToggleButton(String(int(slot)) + String(": None"));
    m_slotButton[slot]->addButtonListener(this);  // make this panel grab the button press      
    addAndMakeVisible(m_slotButton[slot]);
}

现在这就是我想要实现的目标:

  1. 生成 24 个 RadioButtons,内容来自Content = 0: Nonetill 23: None
  2. 单选按钮的生成方式应该是,我们将行分成 3 列,并在每列垂直添加 8 个单选按钮。
  3. 在任何时候,必须只选中一个单选按钮,不得选中其他单选按钮。必须只有一个单击命令可以在相应索引的帮助下处理所有按钮。

请帮忙 :)

4

3 回答 3

2

你不需要你的SlotCommand. 相反,只需将IsChecked每个单选按钮的属性绑定到视图模型上的布尔属性。

例如:XAML:

<RadioButton Content="{Binding SlotButtons}" Margin="0,10,0,0" IsChecked="{Binding IsChecked}" GroupName="SlotGroup" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>

视图模型:

public class SlotViewModel : ViewModelBase
{
    private _isChecked;

    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; NotifyPropertyChanged("IsChecked"); }
    }
}

如果您想在检查时以某种方式做出反应,只需在IsCheckedsetter 中执行即可。

于 2012-10-29T12:09:47.797 回答
0

这是我如何实现的:

XAML:

<Grid Grid.Column="0">
                    <ItemsControl ItemsSource="{Binding SlotChildren}">
                        <ItemsControl.ItemsPanel>
                            <ItemsPanelTemplate>
                                <UniformGrid Columns="3" Rows="8" />
                            </ItemsPanelTemplate>
                        </ItemsControl.ItemsPanel>

                        <ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <RadioButton Content="{Binding SlotButtons}" Margin="0,10,0,0" Command="{Binding SlotCommand}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                            </DataTemplate>
                        </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </Grid>     

视图模型类:

public ObservableCollection<EEPROMSlotViewModel> SlotChildren { get; set; }

SlotChildren = new ObservableCollection<EEPROMSlotViewModel>();
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "0 : None", ID = 0 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "1 : None", ID = 1 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "2 : None", ID = 2 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "3 : None", ID = 3 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "4 : None", ID = 4 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "5 : None", ID = 5 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "6 : None", ID = 6 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "7 : None", ID = 7 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "8 : None", ID = 8 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "9 : None", ID = 9 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "10 : None", ID = 10 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "11 : None", ID = 11 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "12 : None", ID = 12 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "13 : None", ID = 13 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "14 : None", ID = 14 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "15: None", ID = 15 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "16 : None", ID = 16 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "17 : None", ID = 17 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "18 : None", ID = 18 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "19 : None", ID = 19 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "20 : None", ID = 20 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "21 : None", ID = 21 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "22 : None", ID = 22 });
        SlotChildren.Add(new EEPROMSlotViewModel() { SlotButtons = "23 : None", ID = 23 }); 

另一个 ViewModel 类: XAML 的 DataContext 设置为此 ViewModel 类

    /// <summary>
    /// Event for Slot Command Button
    /// </summary>
    private ICommand mSlotCommand;
    public ICommand SlotCommand
    {
        get
        {
            if (mSlotCommand == null)
                mSlotCommand = new DelegateCommand(new Action(mSlotCommandExecuted), new Func<bool>(mSlotCommandCanExecute));

            return mSlotCommand;
        }
        set
        {
            mSlotCommand = value;
        }
    }

    public bool mSlotCommandCanExecute()
    {
        return true;
    }

    public void mSlotCommandExecuted()
    {

    }

但仍然检查所有单选按钮。如何清除它?

于 2012-10-29T10:52:10.630 回答
0

将“组名”明确添加到您的单选按钮。

<RadioButton GroupName="myGroup" Content="{Binding SlotButtons}" Margin="0,10,0,0" Command="{Binding SlotCommand}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center" />

这将只允许检查一个单选按钮。

于 2013-03-18T10:30:00.180 回答