gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 22:24:09 25 4
gpt4 key购买 nike

我正在开发一个 WPF 应用程序,我需要在其中根据按钮单击动态创建 GroupBoxes(其中包含组合框、 slider 和切换按钮)。我的 View 文件夹中有两个 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 时,它应该动态生成 groupbox 4 次并显示它:)

现在这很棘手,每个 Groupbox Header 必须在每个动态生成的 groupbox 中显示不同的名称。假设在启动时,已经显示了 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" });
}

创建以下类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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com