积极答复者
动态创建控件出错.

问题
答案
全部回复
-
我是在这个事件中写的代码.
private void ColumnComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ComboBox combo = sender as ComboBox;
ComboBoxItem comboItem = (ComboBoxItem)combo.SelectedItem;//GridLength height = new GridLength(50);
//GridLength width = new GridLength(200);RowDefinition rowdef = new RowDefinition();
//rowdef.Height = height;
DisplayStyleGrid.RowDefinitions.Add(rowdef);ColumnDefinition coldef = new ColumnDefinition();
//coldef.Width = width;
DisplayStyleGrid.ColumnDefinitions.Add(coldef);
TextBox tb = new TextBox();
tb.Width = 110;
tb.Height = 30;
tb.Text = "aa";DisplayStyleGrid.Children.Add(tb);
DisplayStyleGrid.SetValue(Grid.ShowGridLinesProperty, true);
tb.SetValue(Grid.ColumnProperty, 0);
tb.SetValue(Grid.RowProperty, 0);
}
aaaa2009年7月15日 9:51 -
<UserControl x:Class="SilverlightApplication10.Page" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="100"/> <RowDefinition/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <StackPanel Grid.Row="0" Grid.Column="0" x:Name="DisplayStackPanel"></StackPanel> <ComboBox x:Name="cmbSelect" Grid.Row="0" Grid.Column="1" SelectionChanged="cmbSelect_SelectionChanged"> <ComboBox.Items> <ComboBoxItem Content="1"></ComboBoxItem> <ComboBoxItem Content="2"></ComboBoxItem> </ComboBox.Items> </ComboBox> <Grid x:Name="DisplayStyleGrid"> </Grid> </Grid> </UserControl>
我按你提供的信息,我做了一个,没有出现你说的情况啊!我想是不是我在那里理解上有误吧!
TextBlock textblock = new TextBlock();
textblock.Text = comboItem.Content.ToString();DisplayStackPanel.Children.Add(textblock);//这一步提示,未将对象引用到实例
你这个是在那个方法里调用?我把这个加到cmbSelect_SelectionChanged 事件中试过,也没有出错!
后台代码:namespace SilverlightApplication10 { public partial class Page : UserControl { public Page() { InitializeComponent(); } private void cmbSelect_SelectionChanged(object sender, SelectionChangedEventArgs e) { ComboBox combo = sender as ComboBox; ComboBoxItem comboItem = (ComboBoxItem)combo.SelectedItem; //GridLength height = new GridLength(50); //GridLength width = new GridLength(200); RowDefinition rowdef = new RowDefinition(); //rowdef.Height = height; DisplayStyleGrid.RowDefinitions.Add(rowdef); ColumnDefinition coldef = new ColumnDefinition(); //coldef.Width = width; DisplayStyleGrid.ColumnDefinitions.Add(coldef); //TextBox tb = new TextBox(); //tb.Width = 110; //tb.Height = 30; //tb.Text = "aa"; //DisplayStyleGrid.Children.Add(tb); TextBlock tb = new TextBlock(); tb.Text = comboItem.Content.ToString(); DisplayStackPanel.Children.Add(tb);//这一步提示,未将对象引用到实例 DisplayStyleGrid.SetValue(Grid.ShowGridLinesProperty, true); tb.SetValue(Grid.ColumnProperty, 0); tb.SetValue(Grid.RowProperty, 0); } } }
努力!写一个js解析器,一个svg插件,一个绘图程序,做好自己,呵呵~!2009年7月15日 10:26 -
TextBlock textblock = new TextBlock();
textblock.Text = comboItem.Content.ToString();DisplayStackPanel.Children.Add(textblock);//这一步提示,未将对象引用到实例
Xaml中
<StackPanel Grid.Row="5" Grid.Column="0" x:Name="DisplayStackPanel"></StackPanel>
aaaa
如果您觉得对您有帮助,请在“是否有帮助”点“是”;如果你觉得回复很满意,请“标记为已解答”2009年7月15日 10:39 -
private void ColumnComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
这个是事件,在刚调用childwindow时,就执行了一次,DisplayStyleGrid.RowDefinitions.Add(rowdef);这句话就出错了,
估计是刚启动childwindow时,DisplayStyleGrid并没有实例化,
把动态创建的代码放到一个BUTTON中执行,就可以正常执行.
为什么在这个事件ColumnComboBox_SelectionChanged中不行?
aaaa2009年7月16日 2:05 -
控件是这样的,
<ComboBox x:Name="ColumnComboBox" VerticalAlignment="Center" Width="80" SelectionChanged="ColumnComboBox_SelectionChanged">
<ComboBoxItem Content="1列" Tag="1"></ComboBoxItem>
<ComboBoxItem Content="2列" Tag="2" IsSelected="True"></ComboBoxItem>
<ComboBoxItem Content="3列" Tag="3"></ComboBoxItem>
<ComboBoxItem Content="4列" Tag="4"></ComboBoxItem>
</ComboBox>
aaaa2009年7月16日 2:11