好吧,這是一個棘手的情況。我在我的WPF項目,VoltageChannelView.xaml和VoltageView.Xaml,在我VoltageView.xaml我劃分了網格成3排2個XAML文件如下:動態生成WPF中的UI組件
VoltageView:
<Grid Style="{DynamicResource styleBackground}" >
<Grid.RowDefinitions>
<RowDefinition Height="70" />
<RowDefinition />
<RowDefinition Height="50" />
</Grid.RowDefinitions>
<Grid Grid.Row="0" >
</Grid>
<Grid Name="ContentGrid" Grid.Row="1" Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Name="Label11" />
<RowDefinition Name="Label22" />
<RowDefinition Name="Label33" />
<RowDefinition Name="Label44" />
<RowDefinition Name="Label55" />
<RowDefinition Name="Label66" />
<RowDefinition Name="Label77" />
<RowDefinition Name="Label88" />
<RowDefinition Name="Label99" />
</Grid.RowDefinitions>
</Grid>
<Grid Grid.Row="2" >
<Button Content="Bavaria 2" FontSize="13" Height="25" HorizontalAlignment="Left" Margin="30,0,0,0" Name="RefreshBtn" VerticalAlignment="Center" Width="105" />
<Button Content="Redhook" FontSize="13" Height="25" HorizontalAlignment="Center" Margin="0,0,0,0" Name="Refresh1Btn" VerticalAlignment="Center" Width="105" />
<Button Content="Bavaria 1" FontSize="13" Height="25" HorizontalAlignment="Right" Margin="0,0,30,0" Name="Refresh2Btn" VerticalAlignment="Center" Width="105" />
</Grid>
</Grid>
現在你可以注意到Grid.Row =「1」在那裏我把網格分成9行。
VoltageChannelView:
<CheckBox Content="On" Grid.Column="3" Height="Auto" HorizontalAlignment="Center" Margin="0" Name="On" VerticalAlignment="Center" />
<Button Content="Set" Grid.Column="1" Height="23" HorizontalAlignment="Center" Margin="85,0,0,0" Name="Set" VerticalAlignment="Center" Width="75" />
<TextBox Grid.Column="1" Height="23" HorizontalAlignment="Center" Margin="0,0,80,0" Name="textBox1" VerticalAlignment="Center" Width="70" />
<Label Content="VDD__Main" Grid.Column="0" Height="15" HorizontalAlignment="Center" Margin="0,0,70,0" Name="VoltageLabel" VerticalAlignment="Center" />
<Label Content="0.0 V" Grid.Column="2" Height="15" HorizontalAlignment="Center" Margin="0" Name="CurrentLabel" VerticalAlignment="Center" />
現在我有3個按鈕,在我的Grid.Row = 「2」,我的要求是,當我點擊 「巴伐利亞2」 按鈕,我想VoltageChannelView的內容將被放置在我的VoltageView Grid.Row =「1」。這裏有個訣竅,它應該動態地生成整個內容5次。即在Grid.Row =「1」中出現的9行中,VoltageChannelView的內容應顯示在前5行中,每行中各有一行。
點擊「Bavaria1」應該產生8次內容等等。基本上可以在WPF中根據每個按鈕點擊生成VoltageChannelView「n」次的內容並將其顯示在我的VoltageView中。
雖然實現是一個有點棘手。看起來像'DataTemplate'是這種情況下的關鍵解決方案。 – HichemSeeSharp