首先原谅我的英语不好。
我在使用Grid.SetRow()
or时遇到问题setValue(Grid.RowProperty, rowIndex)
。该元素成为索引,但它们相互重叠..
XAML 定义:
<UserControl x:Class="LangproWPF.FrmSM_ItemsGroup"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Grid.Row="0">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="25"/>
</Grid.RowDefinitions>
<Button Name="btnSM_ItemGroupAdd" Style="{StaticResource ResourceKey=standardButtons}" Grid.Column="0" Grid.Row="1" Click="btnSM_ItemGroupAdd_Click">Wareng. anlegen</Button>
<Button Name="btnSM_ItemGroupShow" Style="{StaticResource ResourceKey=standardButtons}" Grid.Column="1" Grid.Row="1">Wareng. anzeigen</Button>
<Button Name="btnSM_ItemGroupAdd1" Style="{StaticResource ResourceKey=standardButtons}" Grid.Column="0" Grid.Row="2" Click="btnSM_ItemGroupAdd_Click">Wareng. anlegen</Button>
<Button Name="btnSM_ItemGroupShow2" Style="{StaticResource ResourceKey=standardButtons}" Grid.Column="1" Grid.Row="2">Wareng. anzeigen</Button>
<Button Name="btnSM_ItemGroupAdd3" Style="{StaticResource ResourceKey=standardButtons}" Grid.Column="0" Grid.Row="3" Click="btnSM_ItemGroupAdd_Click">Wareng. anlegen</Button>
<Button Name="btnSM_ItemGroupShow4" Style="{StaticResource ResourceKey=standardButtons}" Grid.Column="1" Grid.Row="3">Wareng. anzeigen</Button>
</Grid>
<Grid Name="grdSM_ItemGroupHolder" Grid.Row="1" Background="LightSteelBlue">
</Grid>
</Grid>
</UserControl>
在代码生成的行和内容中
// first remove old elements from grid
grdSM_ItemGroupHolder.Children.Clear();
grdSM_ItemGroupHolder.ColumnDefinitions.Clear();;
// create columns for the Grid
ColumnDefinition colTitle = new ColumnDefinition(){ Width = new GridLength(250)};
ColumnDefinition colControls = new ColumnDefinition();
grdSM_ItemGroupHolder.ColumnDefinitions.Add(colTitle);
grdSM_ItemGroupHolder.ColumnDefinitions.Add(colControls);
grdSM_ItemGroupHolder.ShowGridLines = true;
// define also the first row
RowDefinition row = new RowDefinition();
row.Height = new GridLength(100,GridUnitType.Star);
grdSM_ItemGroupHolder.RowDefinitions.Add(row);
// Form elements
Label lblIGID = new Label(){ Name="lblSM_IGID_info", Content="Die ID wird automatisch vergeben"};
grdSM_ItemGroupHolder.Children.Add(lblIGID);
Grid.SetRow(lblIGID,0);
Grid.SetColumn(lblIGID,0);
Grid.SetColumnSpan(lblIGID,2);
TextBox txtIGID = new TextBox(){ Name="txtSM_ItemGroupID"};
Grid.SetColumn(tbIGID, 0);
Grid.SetRow(tbIGID, 1);
TextBlock tbIGName = new TextBlock(){Name="tbSM_ItemGroupName", Text="Enter a name"};
Grid.SetColumn(tbIGName,0);
Grid.SetRow(tbIGName,1);
Button btnIGReset = new Button() {Name="btnSM_ItemGroupAddReset", Content="Zurücksetzen"};
grdSM_ItemGroupHolder.Children.Add(btnIGReset);
Grid.SetRow(btnIGReset,2);
Grid.SetColumn(btnIGReset,0);
Button btnIGSave = new Button(){Name="btnSM_ItemGroupAddSave", Text="Speichern"};
grdSM_ItemGroupHolder.Children.Add(btnIGSave);
Grid.SetRow(btnIGSave,2);
Grid.SetColumn(btnIGSave,1);
这些行似乎相互重叠,我也只能看到最后两个按钮......其他元素在后面。当我使用此代码添加所有需要的行时:
RowDefinition row1 = new RowDefinition();
row1.Height = new GridLength(100, GridUnitType.Star);
grdSM_ItemGroupHolder.RowDefinitions.Add(row1);
RowDefinition row2 = new RowDefinition();
row2.Height = new GridLength(100, GridUnitType.Star);
grdSM_ItemGroupHolder.RowDefinitions.Add(row2);
有用。我现在正在寻找几个小时..希望有人知道如何解决它?