简体   繁体   English

动态生成标签单击WPF中的动态生成的RadioButton时

[英]Generate Labels Dynamically On clicking Dynamically generated RadioButton in WPF

I am a C++ developer and recently moved to wpf. 我是C ++开发人员,最近搬到wpf。 I seem to have across a tricky situation where I have to dynamically generate labels based on radiobutton click. 我似乎遇到了一个棘手的情况,我必须根据单选按钮的点击动态生成标签。 Here I will show you how I have generated 4 radio buttons first. 在这里,我将向您展示如何首先生成4个单选按钮。

XAML: XAML:

<Grid Grid.Row="0">           

        <ItemsControl ItemsSource="{Binding Children}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical"
                    IsItemsHost="True" />
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>

            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <RadioButton Content="{Binding RadioBase}" Margin="0,10,0,0" IsChecked="{Binding BaseCheck}" Height="15" Width="80" HorizontalAlignment="Center" VerticalAlignment="Center"/>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>          

    </Grid>

<Grid Grid.Row="1">
    <Label Content="{Binding name}" HorizontalAlignment="Center" VerticalAlignment="Center" />      
</Grid>

ViewModel: ViewModel:

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

    private int _ID;
    public int ID
    {
        get
        {
            return _ID;
        }

        set
        {
            _ID = value;
            OnPropertyChanged("ID");
        }
    }

    private string _NAme;
    public string name
    {
        get
        {
            return _NAme;
        }

        set
        {
            _NAme= value;
            OnPropertyChanged("name");
        }
    }

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

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

AnotherViewModel Class: AnotherViewModel类:

public ObservableCollection<FPGAViewModel> Children { get; set; }

    public FPGARadioWidgetViewModel()
    {
        Children = new ObservableCollection<FPGAViewModel>();
        Children.Add(new FPGAViewModel() { RadioBase = "Base 0x0", ID = 0 });
        Children.Add(new FPGAViewModel() { RadioBase = "Base 0x40", ID = 1 });
        Children.Add(new FPGAViewModel() { RadioBase = "Base 0x80", ID = 2 });
        Children.Add(new FPGAViewModel() { RadioBase = "Base 0xc0", ID = 3 });            
    }

This gives me 4 radiobuttons with Content as given above. 这给了我4个带有上述内容的单选按钮。 Now I want to generate 8 labels on each radiobutton click. 现在,我想在每次单选按钮单击上生成8个标签。 I had done this in my C++ app as follows: 我在C ++应用程序中做到了这一点,如下所示:

for(i = 0; i < 0x40 / 8; i++)
{
    reg = (i * 8);
    m_registerLabel[i] = new Label(String::empty, String("Reg 0x") + String::toHexString(reg));
    addAndMakeVisible(m_registerLabel[i]);
}

if you notice, it will create 8 labels with value as Reg 0x0, Reg 0x8, Reg 0x10, Reg 0x18 etc since reg is converted to hexstring. 如果您注意到,它将创建8个标签,其值分别为Reg 0x0, Reg 0x8, Reg 0x10, Reg 0x18等,因为reg已转换为十六进制字符串。 I want to generate something like this when I click Base 0x0 on startup. 当我在启动时单击Base 0x0时,我想生成这样的内容。

How can i achieve this in my app??? 我如何在我的应用程序中实现这一目标?

In your this.sBaseCheck setter you can do this code. 在您的this.sBaseCheck设置程序中,您可以执行以下代码。

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

Add 2 new properties... 添加2个新属性...

  private string[] registerLabels = new string[8];
  public string[] RegisterLabels { get { return registerLabels; } }

  private bool isRegisterItemsVisible = false;
  public bool IsRegisterItemsVisible { 
       get { return isRegisterItemsVisible ; }
       set { 
           isRegisterItemsVisible = value; 
           OnPropertyChanged("IsRegisterItemsVisible"); 
           OnPropertyChanged("RegisterLabels"); 
       }
  }

Populate these values... 填充这些值...

private static void Generatelabels(FPGAViewModel currentItem)
{
  for(i = 0; i < 0x40 / 8; i++)
  {
       reg = (i * 8);
       currentItem.RegisterLabels[i] = "Reg 0x" + String::toHexString(reg);
       currentItem.IsRegisterItemsVisible = true;
   }
}

On your GUI, bind them with another ItemsControl within ItemTemplate of the existing ItemsControl. 在您的GUI上,将它们与现有ItemsControl的ItemTemplate中的另一个ItemsControl绑定。

        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Vertical">
                    <RadioButton 
                       Content="{Binding RadioBase}"
                       Margin="0,10,0,0"
                       IsChecked="{Binding BaseCheck}" 
                       Height="15" Width="80" 
                       HorizontalAlignment="Center" 
                       VerticalAlignment="Center"/>
                    <ItemsControl 
                           Visibility="{Binding IsRegisterItemsVisible,
                                Converter={StaticResource BoolToVisibilityConv}}" 
                           ItemsSource="{Binding RegisterLabels}">   
                       <ItemsControl.ItemTemplate>
                          <DataTemplate>
                             <TextBlock Text="{Binding}"/>
                           </DataTemplate>
                       </ItemsControl.ItemTemplate>
                    </ItemsControl>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM