0

こんにちは仲間:)私はグループボックスとラップパネルを扱うwpfアプリに取り組んでいます。タイトルを見ると簡単そうに見えますが、グループボックスのセットを動的に生成した後、難しいと感じています。シナリオは次のとおりです。

プロジェクトに2つのxamlファイルがあります。1つはとCodecView.xamlですCodecWidgetView.xaml。次のように、起動時に4つのグループボックスを動的に生成しました。

CodecView.xaml:

<UserControl.Resources>
    <DataTemplate x:Key="CWDataTemplate">
        <WrapPanel>
            <TextBlock Text="{Binding Description}" Margin="0,0,0,0"/>
            <local:CodecWidgetView Margin="5,10,5,5"/>
        </WrapPanel>
    </DataTemplate>
</UserControl.Resources>

<Grid Grid.Row="0" >          
        <Grid Name="NumberofCodecs" >
            <ItemsControl ItemTemplate="{StaticResource CWDataTemplate}" ItemsSource="{Binding CodecWidgets}"/>
        </Grid>            
    </Grid> 

CodecWidgetView.xaml:

<Grid>
    <GroupBox Height="Auto" HorizontalAlignment="Stretch" Margin="5,5,5,5" 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" ItemsSource="{Binding ModesList}" SelectedItem="{Binding SelectedModesList, Mode=OneWayToSource}" SelectedIndex="2" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox2" VerticalAlignment="Center" Width="80" />
                <ComboBox Grid.Column="0" ItemsSource="{Binding FrequencyList}" SelectedItem="{Binding SelectedFrequencyList, Mode=OneWayToSource}" SelectedIndex="0" Height="23" HorizontalAlignment="Center" Margin="0,0,0,0" Name="comboBox1" VerticalAlignment="Center" Width="80" />
            </Grid>

            <Grid Grid.Row="1">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Command="{Binding OneSixBitCommand}" Margin="0" Content="16 Bit" Name="OneSixBit" Height="25" Width="45" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" />
                <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Command="{Binding TwoZeroBitCommand}" Margin="0" Content="20 Bit" Name="TwoZeroBit" Height="25" Width="45" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" />
                <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Command="{Binding TwoFourBitCommand}" Margin="0" Content="24 Bit" Name="TwoFourBit" Height="25" Width="45" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" />
                <RadioButton Style="{StaticResource {x:Type ToggleButton}}" Command="{Binding ThreeTwoBitCommand}" Margin="0" Content="32 Bit" Name="ThreetwoBit" Height="25" Width="45" Grid.Column="3" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </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, StringFormat=0.0}" 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, StringFormat=0.0}" 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>
</Grid>

CodecViewModel.cs:CodecView.xamlのviewmodelクラスです

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

    public CodecViewModel()
    {
        CodecWidgets = new ObservableCollection<CodecWidgetViewModel>();
        CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 8  - Dig Mic A" });
        CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 9  - Dig Mic B" });
        CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 10  - PCM A 3P3V" });
        CodecWidgets.Add(new CodecWidgetViewModel { Description = "Location 11  - PCM B 3P3V" });
    }

CodecWidgetViewModel.cs:CodecWidgetView.xamlのviewmodelクラスです

private string _description;
    public string Description
    {
        get
        { 
            return _description; 
        }

        set
        {
            _description = value;
            OnPropertyChanged("Description");
        }
    }

これにより、起動時に4つのグループボックスが上下に動的に生成されます。私のウィンドウサイズはminheight=300およびminwidth=300なので、scrollviewerを設定しました。Wrappanelを使用したので、伸ばすと期待どおりに動作するはずです。そのとき、幅が拡大されます。2番目のグループボックスは1番目の行の右側に移動する必要があり、同じことが下でも発生します。

始めるとき: 始めるとき

幅を伸ばす場合:ここに画像の説明を入力してください

期待される動作:ここに画像の説明を入力してください

したがって、予想される動作を見て、wrappanelの動作を実現したいと思います:) wrappanelを使用したことがありますが、期待どおりに動作しません。助けてください :)

4

1 に答える 1

1

あなたは、WrapPanel を ItemsSource の個々のアイテムのパネルとして使用しましたが、これはあなたが望んでいることではありません。

代わりに、すべての子のパネルとして WrapPanel を使用するように ItemsControl に明示的に指示する必要があります。

<ItemsControl ItemTemplate="{StaticResource CWDataTemplate}" ItemsSource="{Binding CodecWidgets}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
</ItemsControl>
于 2012-10-18T07:03:48.450 に答える