生成RadioButtons与不同的'内容' [英] Generate RadioButtons With Different 'Content'
问题描述
我是一个C ++开发人员,目前正在开发一个WPF应用程序,我必须动态生成4个单选按钮,每个按钮将有不同的标题名称。我遵循MVVM模式。
< Grid Grid.Row =0
< Grid.RowDefinitions>
< RowDefinition />
< RowDefinition Height =35/>
< /Grid.RowDefinitions>
< RadioButton Grid.Row =0Content ={Binding RadioBase}IsChecked ={Binding BaseCheck}Height =15Width =80HorizontalAlignment = =0,0,0,0VerticalAlignment =Center/>
< Button Grid.Row =1Content =Refresh RegsHorizontalAlignment =CenterVerticalAlignment =CenterMargin =0Width =100Height =25/>
< / Grid>
现在如果你注意到我的XAMl,我只有一个单选按钮在我的 Grid.Row =0
。理想情况下,我想生成它4次并设置绑定到其内容
和 IsChecked
,以便我给我4不同内容。
ViewModel :
private bool sBaseCheck;
public bool BaseCheck
{
get {return this.sBaseCheck; }
set
{
this.sBaseCheck = value;
this.OnPropertyChanged(BaseCheck);
}
}
私有字符串_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次,并有一个buttonclick事件。它创建Title如下:
- 按钮1 =基本0x0(因为i = 0,toHexString将0x0转换为0)
- 按钮2 =基本0x40(自i = 1且toHexString将0x40转换为
40) - 按钮3 =基本0x80(因为i = 2和toHexString按钮4 =基本0xc0(因为i = 3,toHexString将
0xc0转换为c0)
/ ul>
如何在我的WPF应用程序中实现这样? :)我会感激,如果你们帮我解决这个? :)
感谢
一个完整的示例:
模型:
命名空间WpfApplication1
{
public class RB
{
public bool BaseCheck {get;组; }
public string RadioBase {get;组; }
}
}
RBVM:
命名空间WpfApplication1
{
public class RBVM:INotifyPropertyChanged
{
public RBVM b $ b {
_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);
}
}
公共事件PropertyChangedEventHandler PropertyChanged ;
#region方法
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
{
处理程序(this,new PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
}
ViewModel:
命名空间WpfApplication1
{
public class RBViewModel:INotifyPropertyChanged
{
public void AddRb(string content,bool isChk)
{
_rbs.Add(new RBVM(){RadioBase = content,BaseCheck = isChk});
}
public void ClearAllValues(){
foreach(_rbs中的RBVM项)
{
item.BaseCheck = false;
}
}
public RBVM GetChecked(){
foreach(_rbs中的RBVM项)
{
if item.BaseCheck){
return item;
}
}
return null;
}
private ObservableCollection< RBVM> _rbs = new ObservableCollection< RBVM>();
public ObservableCollection< RBVM> Rbs
{
get
{
return _rbs;
}
set
{
_rbs = value;
}
}
公共事件PropertyChangedEventHandler PropertyChanged;
#region方法
private void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if(handler!= null)
{
处理程序(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 =MainWindow1Height =300Width =300>
< Window.DataContext>
< local:RBViewModel />
< /Window.DataContext>
< Window.Resources>
< DataTemplate x:Key =RadioDataTemplate>
< StackPanel>
< RadioButton GroupName =someGroupContent ={Binding RadioBase}IsChecked ={Binding BaseCheck}Height =15Width =80HorizontalAlignment =CenterVerticalAlignment =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 =50Height =10x:Name =testClick =test_Click>< / Button&
< / Grid>
< / Window>
XAML代码背后:
命名空间WpfApplication1
{
///< summary>
/// MainWindow1.xaml的交互逻辑
///< / summary>
public partial class MainWindow1:Window
{
RBViewModel _viewModel;
public MainWindow1()
{
InitializeComponent();
_viewModel =(RBViewModel)bas.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();
}
}
}
I am a C++ developer and currently working on a WPF app where I have to generate 4 radio buttons dynamically and each button will have different title name. I am following MVVM pattern.
<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>
Now if you notice my XAMl, I have just One radio button in my Grid.Row="0"
. Ideally I would want to generate it 4 times and set a binding to its Content
and IsChecked
so that I gives me 4 different Content
.
ViewModel:
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");
}
}
I had done this in my C++ application as follows:
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);
}
If you notice here, It creates it 4 times and has one buttonclick event. It creates Title as follows:
- Button 1 = Base 0x0 (since i = 0 and toHexString converts 0x0 to 0)
- Button 2 = Base 0x40 (since i = 1 and toHexString converts 0x40 to 40)
- Button 3 = Base 0x80 (since i = 2 and toHexString converts 0x80 to 80)
- Button 4 = Base 0xc0 (since i = 3 and toHexString converts 0xc0 to c0)
How can I achieve like this in My WPF application? :) I would appreciate if you guys help me solve this? :)
Thanks
Based on yours comment, I created a complete example :
Model:
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
}
}
ViewModel:
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 Code Behind:
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();
}
}
}
这篇关于生成RadioButtons与不同的'内容'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!