在XMAL中生成自定义类的对象并与控件绑定

来源:互联网 发布:手机淘宝买东西步骤 编辑:程序博客网 时间:2024/05/21 06:26
 自定义类

namespace MyClassLibrary

{

    public class MyClass

    {

        public string MyName { get; set; }

    }

}

在UI工程添加自定义库的引用

在XMAL中创建资源,并与UI控件绑定

<Window x:Class="MyLibrary.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:myclass="clr-namespace:MyClassLibrary;assembly=MyClassLibrary"

        Title="MainWindow" Height="100" Width="200">

    <Window.Resources>

        <myclass:MyClass x:Key="myClass" MyName="Kyran"/>

    </Window.Resources>

    <Grid>

        <TextBox Text="{Binding Source={StaticResource myClass}, Path=MyName}"/>

    </Grid>

</Window>