深入浅出WPF 第一部分(3)

3.2.3 属性元素

    <Grid HorizontalAlignment="Center" VerticalAlignment="Center">
        <Rectangle x:Name="rectangle" Width="200" Height="120">
            <Rectangle.Fill>
                <SolidColorBrush Color="Blue" />
            </Rectangle.Fill>
        </Rectangle>
    </Grid>
3.2.4 标记扩展(Markup Extensions)

<Window x:Class="FirstWpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Anders" Height="120" Width="296">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="10*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="33"/>
            <RowDefinition Height="33"/>
        </Grid.RowDefinitions>
        <TextBox Name="textBox1" Grid.Row="0" Grid.Column="0" Margin="5"/>
        <TextBox Name="textBox2" Grid.Row="1" Grid.Column="0" Margin="5" Text="{Binding ElementName=textBox1, Path=Text, Mode=TwoWay}"/>
        <!--textBox2的Text属性双向绑定到textBox1的Text属性-->
    </Grid>
</Window>

3.3 事件处理器与代码后置

这样的将逻辑代码与UI代码分离、隐藏在UI代码后面的形式叫做代码后置.(code behind)

VS IDE提供了一个事件的IDE feature,在xaml中右键点击事件名称,能够“Navigate to Event Hander”

x:Code,使用它能够把本来应该呆在后置代码里的C#代码搬到XAML文件中来。x:Code的内容一定要使用XML语言的<![[CDATA[...]]>转义标签。

 <Grid>
        <Button Name="button1" Margin="5" Click="button1_Click"/>
    </Grid>
    <x:Code>
        <![CDATA[
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                MessageBox.Show("button1 clicked...");
            }
        ]]>
    </x:Code>

3.4 导入程序集和引用当中的名称空间

在XAML中引用名称空间的语法是:

xmlns:映射名=“clr-namespace:类库中名称空间的名字;assembly=类库文件名称”

xmlns:my="clr-namespace:MyNamespace;assembly=ClassLibrary1"


原文地址:https://www.cnblogs.com/gcczhongduan/p/4172463.html