wpf 怎么让页面首次加载时不给文本框添加样式,而是在输入后添加
1个回答
展开全部
要想添加控件,容器是必须要有的,几大布局控件都可以 Grid Canvas UniformGrid DockPanel StackPanel WrapPanel 下面就以WrapPanel 为例进行演示
先上代码
[csharp] view plain copy
/// <summary>
/// 添加控件的方法
/// </summary>
/// <param name="Container"> Pannel类的实例</param>
/// <param name="Control">UIElement 实例</param>
public static void DynamicAdd(Panel Container, UIElement Control)
{
Container.Children.Add(Control);
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
Button btn = new Button();
btn.Width = 200;
btn.Height = 70;
//设置按钮样式使用此种方法必须将资源文件引用到当前页面
btn.Style = Resources["NoticeButton"] as Style;
//此种方法也可以设置样式
//获取App.xaml中的样式个人更喜欢这种
//Style style = (Style)this.FindResource("NoticeButton");
//为按钮设置样式
// btn.Style = style;
//将btn添加到ufg
DynamicAdd(wrp, btn);
}
这里是后台的代码很简单只是在设置样式的时候要注意一下用哪种方法
前台xaml代码主要是对资源的引用
[csharp] view plain copy
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Css/EventsButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<WrapPanel Name="wrp" Grid.Row="0"></WrapPanel>
<Button Content="添加" Grid.Row="1" Height="23" HorizontalAlignment="Center" Name="buttonAdd" VerticalAlignment="Top" Width="75" Click="buttonAdd_Click" />
</Grid>
顶
先上代码
[csharp] view plain copy
/// <summary>
/// 添加控件的方法
/// </summary>
/// <param name="Container"> Pannel类的实例</param>
/// <param name="Control">UIElement 实例</param>
public static void DynamicAdd(Panel Container, UIElement Control)
{
Container.Children.Add(Control);
}
private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
Button btn = new Button();
btn.Width = 200;
btn.Height = 70;
//设置按钮样式使用此种方法必须将资源文件引用到当前页面
btn.Style = Resources["NoticeButton"] as Style;
//此种方法也可以设置样式
//获取App.xaml中的样式个人更喜欢这种
//Style style = (Style)this.FindResource("NoticeButton");
//为按钮设置样式
// btn.Style = style;
//将btn添加到ufg
DynamicAdd(wrp, btn);
}
这里是后台的代码很简单只是在设置样式的时候要注意一下用哪种方法
前台xaml代码主要是对资源的引用
[csharp] view plain copy
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/Css/EventsButton.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
<RowDefinition Height="30"></RowDefinition>
</Grid.RowDefinitions>
<WrapPanel Name="wrp" Grid.Row="0"></WrapPanel>
<Button Content="添加" Grid.Row="1" Height="23" HorizontalAlignment="Center" Name="buttonAdd" VerticalAlignment="Top" Width="75" Click="buttonAdd_Click" />
</Grid>
顶
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询