c# - 生成具有不同 'Content' 的单选按钮

标签 c# c++ .net wpf radio-button

我是一名 C++ 开发人员,目前正在开发一个 WPF 应用程序,我必须在其中动态生成 4 个单选按钮,并且每个按钮都有不同的标题名称。我遵循 MVVM 模式。

<Grid Grid.Row="0">
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="35" />                
        </Grid.RowDefinitions>

        <RadioButton Grid.Row="0" Content="{Binding RadioBase}" IsChecked="{Binding BaseCheck}" Height="15" Width="80" HorizontalAlignment="Center" Margin="0,0,0,0" VerticalAlignment="Center" />
        <Button Grid.Row="1" Content="Refresh Regs" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0" Width="100" Height="25" />
</Grid>

现在,如果您注意到我的 XAMl,我的 Grid.Row="0" 中只有一个单选按钮。理想情况下,我希望生成它 4 次并为其 ContentIsChecked 设置绑定(bind),这样我就可以得到 4 个不同的 Content

View 模型:

private bool sBaseCheck;
    public bool BaseCheck
    {
        get { return this.sBaseCheck; }
        set
        {
            this.sBaseCheck= value;
            this.OnPropertyChanged("BaseCheck");
        }
    }

private string _RadioBase;
    public string RadioBase
    {
        get
        {
            return _RadioBase;
        }

        set
        {
            _RadioBase= value;
            OnPropertyChanged("RadioBase");
        }
    }

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

for(i = 0; i < 4; i++)
{
    m_registerBase[i] = new ToggleButton(("Base 0x")+String::toHexString(i * 0x40));        
    addAndMakeVisible(m_registerBase[i]);
    m_registerBase[i]->addButtonListener(this);
}

如果您注意到这里,它创建了 4 次并且有一个按钮单击事件。它按如下方式创建标题:

  • 按钮 1 = Base 0x0(因为 i = 0 并且 toHexString 将 0x0 转换为 0)
  • Button 2 = Base 0x40(因为 i = 1 并且 toHexString 将 0x40 转换为 40)
  • Button 3 = Base 0x80(因为 i = 2 并且 toHexString 转换为 0x80 到 80)
  • Button 4 = Base 0xc0(因为 i = 3 和 toHexString 转换 0xc0 到 c0)

如何在我的 WPF 应用程序中实现这样的目标? :) 如果你们帮我解决这个问题,我将不胜感激? :)

谢谢

最佳答案

根据您的评论,我创建了一个完整的示例:

型号:

    namespace WpfApplication1
    {
        public class RB
        {
            public bool BaseCheck { get; set; }
            public string RadioBase { get; set; }
        }
    }

RBVM:

        namespace WpfApplication1
        {
            public class RBVM : INotifyPropertyChanged
            {

                public RBVM()
                {
                    _rb = new RB();
                }

               private RB _rb;
                public RB RB
               {
                   get
                   {
                       return _rb;
                   }
                   set
                   {
                       _rb = value;
                   }
               }

                public bool BaseCheck
                {
                    get
                    {
                        return RB.BaseCheck;
                    }
                    set
                    {
                        RB.BaseCheck = value;
                        RaisePropertyChanged("BaseCheck");
                    }
                }

                public string RadioBase
                {
                    get
                    {
                        return RB.RadioBase;
                    }
                    set
                    {
                        RB.RadioBase = value;
                        RaisePropertyChanged("RadioBase");
                    }
                }








                public event PropertyChangedEventHandler PropertyChanged;

                                                    #region Methods

            private void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
            }
        }

View 模型:

     namespace WpfApplication1
    {
        public class RBViewModel : INotifyPropertyChanged
        {

            public void AddRb(string content, bool isChk) 
            {
                _rbs.Add(new RBVM() { RadioBase = content, BaseCheck = isChk });
            }


            public void ClearAllValues() {
                foreach (RBVM item in _rbs)
                {
                    item.BaseCheck = false;
                }

            }

            public RBVM GetChecked() {
                foreach (RBVM item in _rbs)
                {
                    if (item.BaseCheck) {
                        return item;
                    }
                }

                return null;

            }


            private ObservableCollection<RBVM> _rbs = new ObservableCollection<RBVM>();


            public ObservableCollection<RBVM> Rbs
           {
               get
               {
                   return _rbs;
               }
               set
               {
                   _rbs = value;
               }
           }



            public event PropertyChangedEventHandler PropertyChanged;

            #region Methods

            private void RaisePropertyChanged(string propertyName)
            {
                PropertyChangedEventHandler handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propertyName));
                }
            }
            #endregion
        }
    }

XAML

    <Window x:Class="WpfApplication1.MainWindow1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication1"
            Title="MainWindow1" Height="300" Width="300">

        <Window.DataContext>
            <local:RBViewModel />
        </Window.DataContext>
        <Window.Resources>

            <DataTemplate x:Key="RadioDataTemplate">
                <StackPanel>
                    <RadioButton GroupName="someGroup" Content="{Binding RadioBase}" IsChecked="{Binding BaseCheck}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center" />
                </StackPanel>
            </DataTemplate>

        </Window.Resources>

        <Grid x:Name="main">

            <ItemsControl ItemsSource="{Binding Rbs}" ItemTemplate="{StaticResource RadioDataTemplate}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel x:Name="radios" />
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>

            <Button Width="50" Height="10" x:Name="test" Click="test_Click" ></Button>
        </Grid>

    </Window>

XAML 代码隐藏:

          namespace WpfApplication1
    {
        /// <summary>
        /// Interaction logic for MainWindow1.xaml
        /// </summary>
        public partial class MainWindow1 : Window
        {
            RBViewModel _viewModel;

            public MainWindow1()
            {
                InitializeComponent();

                _viewModel = (RBViewModel)base.DataContext;


                for (int i = 0; i < 4; i++)
                {
                    _viewModel.AddRb("a" + i, true);

                }
            }

            private void test_Click(object sender, RoutedEventArgs e)
            {
                Console.WriteLine(_viewModel.GetChecked().RadioBase);
                _viewModel.ClearAllValues();
            }



        }


    }

关于c# - 生成具有不同 'Content' 的单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13063391/

相关文章:

c# - 求csv文件中每一列的最大长度

c# - 每个 View 模型的 Autofac 实例

c# - 使用 AuthorizeAttribute 进行 MVC 集成测试

c++ - 内存管理的正确方式

c++ - 如何使用 GNU Make 从同一源代码编译程序的第二个版本?

c# - IOrderedQueryable 跳过并获取

c++ - conan.io - 仅 header 包

c# - .NET 不信任我的自签名证书,但 IE 信任?

c# - 获取不同 .NET 版本的 machine.config 路径的最佳方法

.net - 使用 SolrNet 查询 Solr 时,查看请求的实际 Url 的最简单方法是什么?