c# - 在 WPF 中单击按钮动态生成多个 GroupBox

标签 c# .net wpf mvvm groupbox

我正在开发一个 WPF 应用程序,我需要根据按钮单击动态创建 GroupBox(其中包含组合框、 slider 和切换按钮)。我的查看文件夹中有两个 xaml 文件。 “CodecView.xaml”和“CodecWidgetView.xaml”。

CodecView.XAML:

    <Grid>
        <ScrollViewer Name="GroupBoxScroll" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" >
            <Grid Name="NumberofCodecs" Style="{DynamicResource styleBackground}" />                  
        </ScrollViewer>            
    </Grid>

    <Button Content="Add Box" Name="AddBoxBtn" Command="{Binding AddGroupBoxCommand}" />        

CodecWidgetView.xaml:

<GroupBox Header="{Binding GroupBoxHeader}" Height="Auto" HorizontalAlignment="Stretch" Margin="5,5,0,0" Name="groupBox1" VerticalAlignment="Stretch" Width="Auto">
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>

            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <ToggleButton Name="FrequencyBox" Content="Master" Grid.Column="1" Height="25" Width="50" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" />
                <ComboBox Grid.Column="2" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox2" VerticalAlignment="Center" Width="80" />
                <ComboBox Grid.Column="0" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox1" VerticalAlignment="Center" Width="80" />
            </Grid>
            s
            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <ToggleButton Name="OneSixBit" Content="16 Bit" Grid.Column="0" Height="25" Width="45" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" />
                <ToggleButton Name="ThreeTwoBit" Content="32 Bit" Grid.Column="3" Height="25" Width="45" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" />
                <ToggleButton Name="TwentyBit" Content="20 Bit" Grid.Column="1" Height="25" Width="45" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" />
                <ToggleButton Name="TwentyFourBit" Content="24 Bit" Grid.Column="2" Height="25" Width="45" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0" />
            </Grid>

            <Grid Grid.Row="2">
                <Label Name="BitDelay" Content="Bit Delay" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,205,0" Height="25" Width="55" />
                <Slider Height="23" HorizontalAlignment="Center" Minimum="0.0" Maximum="255.0" TickFrequency="1.0" Margin="95,0,0,0" Name="bitdelayslider" VerticalAlignment="Center" Width="160" />
                <TextBox Name="BitDelayValue" IsReadOnly="True" Text="{Binding ElementName=bitdelayslider,Path=Value}" Width="40" Height="20" Margin="0,0,110,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Grid>

            <Grid Grid.Row="3">
                <Label Name="DBGain" Content="DB Gain" VerticalAlignment="Center" HorizontalAlignment="Center" Margin="0,0,205,0" Height="25" Width="55" />
                <TextBox Name="DBGainValue" IsReadOnly="True" Text="{Binding ElementName=dbgainslider,Path=Value}" Width="40" Height="20" Margin="0,0,110,0" HorizontalAlignment="Center" VerticalAlignment="Center" />
                <Slider Height="23" HorizontalAlignment="Center" Minimum="0.0" Maximum="59.5" TickFrequency="0.5" Margin="95,0,0,0" Name="dbgainslider" VerticalAlignment="Center" Width="160" />
            </Grid>
        </Grid>
    </GroupBox>

CodecViewModel:是CodecView.xaml的 View 模型

    /// <summary>
    /// Event for Refresh Button
    /// </summary>
    private ICommand mAddGroupBoxCommand;
    public ICommand AddGroupBoxCommand
    {
        get
        {
            if (mAddGroupBoxCommand == null)
                mAddGroupBoxCommand = new DelegateCommand(new Action(mAddGroupBoxCommandExecuted), new Func<bool>(mAddGroupBoxCommandCanExecute));

            return mAddGroupBoxCommand;
        }
        set
        {
            mAddGroupBoxCommand = value;
        }
    }

    public bool mAddGroupBoxCommandCanExecute()
    {
        return true;
    }

    public void mAddGroupBoxCommandExecuted()
    {
        //Here It should display the groupbox 4 times
    }

模型类:

private string GroupBoxHeaderName;
    public string GroupBoxHeader
    {
        get
        {
            return GroupBoxHeaderName;
        }

        set
        {
            GroupBoxHeaderName = value;
            OnPropertyChanged("GroupBoxHeader");
        }
    }

因此,我想在启动时将 CodecWidgetView.xaml 中存在的此 Groupbox 添加到 CodecView.xaml 中的网格(NumberofCodecs)。当我单击 AddBoxButton 时,它应该动态生成组框 4 次并显示它:)

现在这很棘手,每个组框标题必须在每个动态生成的组框中显示不同的名称。假设在启动时,已显示 2 个组框,其中 Groupbox Header = "Location 1"GroupBox Header = "Location 2"。在 AddgroupBox 按钮上,单击我想要 4 个组框,其标题为 Groupbox Header = "Location 3"Groupbox Header = "Location 4"Groupbox Header = "Location 5"Groupbox Header = "Location 6"“

这可能吗? :)

最佳答案

在下面的代码中,我在“CodecView.xaml”中获取一个项目控件,对于该项目控件,ItemTemplate 是您的“CodecWidgetView.Xaml”,并向该数据模板添加了描述。我创建了另一个类 CodecWidgetViewModel.cs ,它将作为“CodecWidgetView” View 的 View 模型。

在“CodecViewModel”的构造函数中,我正在为“CodecWidgetViewModel”创建实例,并将它们添加到可观察集合中,该集合是“CodecView”中 ItemsControl 的源。

所以此时它将生成 2 个 CodecWidgetViews.. 单击按钮时我将添加 4 个实例,因此它将生成 4 个 CodecWidgetViews.. 您可以根据您的要求修改“mAddGroupBoxCommandExecuted”方法中的代码..

点击按钮

CodecView.XAML

<UserControl>
    <UserControl.Resources>
            <DataTemplate x:Key="CWDataTemplate">
                <StackPanel>
                    <TextBlock Text="{Binding Description}"/>
                <local:CodecWidgetView/>
                    </StackPanel>
            </DataTemplate>
    </UserControl.Resources>
        <Grid>
            <Grid>
                <ScrollViewer Name="GroupBoxScroll" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0" >
                    <Grid Name="NumberofCodecs" Style="{DynamicResource styleBackground}" >
                        <ItemsControl ItemTemplate="{StaticResource CWDataTemplate}" ItemsSource="{Binding CodecWidgets}"/>
                    </Grid>
                </ScrollViewer>
            </Grid>

            <Button Content="Add Box" Name="AddBoxBtn" Command="{Binding AddGroupBoxCommand}" Click="AddBoxBtn_Click" HorizontalAlignment="Right" VerticalAlignment="Bottom"  />
        </Grid>
</UserControl>

CodecViewModel.cs

创建这样的属性

  public ObservableCollection<CodecWidgetViewModel> CodecWidgets { get; set; }

并将以下代码添加到您的 CodecViewModel 构造函数

 CodecWidgets = new ObservableCollection<CodecWidgetViewModel>();
            CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 1"});
            CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 2" });

添加小部件

public void mAddGroupBoxCommandExecuted()
    {
         CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 3" });
            CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 4" });
            CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 5" });
            CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 6" });
    }

创建以下类(class) CodecWidgetViewModel.cs

public class CodecWidgetViewModel 
    {
        private string _description;
        public string Description {

            get { return _description; }
            set {
                _description = value;                   
            }
        }
    }

关于c# - 在 WPF 中单击按钮动态生成多个 GroupBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12930489/

相关文章:

c# - 我可以在命令中调用命令吗?

wpf - 在一个项目中编译 Silverlight 和 WPF 的最佳做法是什么?

wpf - 使用LINQ vs CollectionView过滤集合

c# - 为什么 Azure 函数发送永无休止的 HTTP 请求?

c# - 如何在 ASP-NET C# 中从特定路径打开文件?

.NET/WinForms : maximize a window on a specific screen

c# - C# 中的 "protected"方法?

c# - 使 Controller 异步的最简单方法

c# - 按钮的继承

c# - 如果 StreamWriter 变量在 if 语句内初始化并作为参数传递给 Console.SetOut(),它是否会过早地被 GC 处理?