WPF C#代码中引用XAML中的控件
Posted bcbobo21cn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF C#代码中引用XAML中的控件相关的知识,希望对你有一定的参考价值。
先在XAML中做一个Grid布局,运行如下;此grid有Name和ShowGridLines两个属性;
可在C#代码中引用此grid,调用其方法,设置其属性;
为Button添加单击事件,在C#代码中写如下,
private void Button_Click_1(object sender, RoutedEventArgs e)
grid1.ShowGridLines = false;
grid1.Background = Brushes.Green;
运行如下;
XAML代码;
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid Name="grid1" ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition ></RowDefinition>
<RowDefinition ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="50"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0" Margin="3">Left</Button>
<Button Grid.Row="0" Grid.Column="2" Margin="3">Right</Button>
<Button Grid.Row="1" Grid.Column="0" Margin="3" Click="Button_Click_1">Left</Button>
<Button Grid.Row="1" Grid.Column="2" Margin="3">Right</Button>
<GridSplitter Grid.Row="0" Grid.Column="1" Grid.RowSpan="2" Width="3" VerticalAlignment="Stretch"
HorizontalAlignment="Center" ShowsPreview="False"></GridSplitter>
</Grid>
</Window>
以上是关于WPF C#代码中引用XAML中的控件的主要内容,如果未能解决你的问题,请参考以下文章
C# WPF中xaml怎么调用.cs中的数据?反过来怎么调用?