2

我正在开发一个 WPF 应用程序,我需要在其中基于按钮单击动态创建 GroupBoxes(包含组合框、滑块和切换按钮)。我的视图文件夹中有两个 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
    }

模型类:

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

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

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

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

可能吗?:)

4

1 回答 1

3

在下面的代码中,我在“CodecView.xaml”中使用了一个 itemscontrol,对于该 itemscontrol,ItemTemplate 是您的“CodecWidgetView.Xaml”,并向该数据模板添加了描述。我创建了另一个类 CodecWidgetViewModel.cs,它将成为“CodecWidgetView”视图的视图模型。

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

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

点击按钮

编解码器视图.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;                   
            }
        }
    }
于 2012-10-17T10:21:18.833 回答