【问题标题】:Generate Labels Dynamically On clicking Dynamically generated RadioButton in WPF在 WPF 中单击动态生成的 RadioButton 时动态生成标签
【发布时间】:2012-10-15 13:21:57
【问题描述】:

我是一名 C++ 开发人员,最近转到 wpf。我似乎遇到了一个棘手的情况,我必须根据单选按钮单击动态生成标签。这里我将首先向您展示我是如何生成 4 个单选按钮的。

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>

视图模型:

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");
        }
    }

另一个ViewModel类:

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 });            
    }

这给了我 4 个单选按钮,内容如上所示。现在我想在每次单击单选按钮时生成 8 个标签。我在我的 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]);
}

如果您注意到,它将创建 8 个标签,其值为 Reg 0x0, Reg 0x8, Reg 0x10, Reg 0x18 等,因为 reg 已转换为十六进制字符串。我想在启动时单击Base 0x0 时生成类似的东西。

如何在我的应用中实现这一点???

【问题讨论】:

标签: c# .net wpf mvvm radio-button


【解决方案1】:

在您的 this.sBaseCheck 设置器中,您可以执行此代码。

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

添加 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"); 
       }
  }

填充这些值...

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;
   }
}

在您的 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>

【讨论】:

  • 感谢您的回复。它在currentItem.RegisterLabel[i] = String("Reg 0x") + String::toHexString(reg);ViewModel doesnt contain definition for RegisterLabel.String 上抛出错误string is a type but is used like a variable.
  • 已更新。请假设这是伪代码,因为我不是 C++ 开发人员。
  • 谢谢兄弟.....我删除了所有错误,但应用程序正在崩溃并抛出以下内容:Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception.。我猜 xaml 有问题
  • 嘿嗨,BoolToVisibilityConv 是一个 WPF IValueConverter 实现,用于将布尔值转换为可见性。是你写的吗?
  • 我用过,效果很好,但是有个bug。如果我单击第一个单选按钮,则底部 3 个按钮不可见,如果我单击第二个单选按钮,则最后 2 个按钮不可见,依此类推。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
  • 2012-10-21
  • 2015-05-19
  • 1970-01-01
  • 2017-09-29
  • 1970-01-01
相关资源
最近更新 更多