在Button上动态生成多个GroupBox在WPF中单击

时间:2012-10-17 08:49:28

标签: c# .net wpf mvvm groupbox

我正在开发一个WPF应用程序,我需要根据Button Click动态创建GroupBoxes(包含组合框,滑块和togglebutton)。我的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的视图模型

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

ModelClass:

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

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

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

现在这很棘手,每个Groupbox标头必须在每个动态生成的组框中显示不同的名称。让我们说在启动时,已经有Groupbox Header = "Location 1"GroupBox Header = "Location 2"显示了2个组框。在AddgroupBox button点击后,我希望将4个分组框的标题设为Groupbox Header = "Location 3" Groupbox Header = "Location 4" Groupbox Header = "Location 5" Groupbox Header = "Location 6"

有可能吗? :)

1 个答案:

答案 0 :(得分:3)

在下面的代码中,我在“CodecView.xaml”中进行了itemscontrol,对于itemscontrol,ItemTemplate是你的“CodecWidgetView.Xaml”,并为该datatemplate添加了描述。我已经创建了另一个类CodecWidgetViewModel.cs,它将是“CodecWidgetView”视图的视图模型。

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