WPF的xaml中导入其他命名空间以及绑定类属性数据

 

在window 或者page的属性标签内添加xmlns:c ="clr-namespace:WpfApplication1" 就可以了

表示导入了WpfApplication1这个命名空间,通过字母c来开头引用,比如在资源定义中使用

<c:Test x:key="t">其中test类就是wpfapplication1中的类

 

有两种方式绑定类的属性数据

这是wpfapplication1中的一个类

 public class Test
    {
        private string _color;

        public string Color
        {
            get
            {
                return "red";
            }
        }
    }

 

这是xaml文件

<Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="297" Width="708" Loaded="Window_Loaded"
    xmlns:c ="clr-namespace:WpfApplication1"
    >
    <Window.Resources>
        <c:Test x:Key="t"></c:Test>
    </Window.Resources>
  
        <Window.DataContext>
        <Binding Source="{StaticResource  t}"></Binding>
    </Window.DataContext>
    <Grid>
       <Button Background="{Binding Path=Color}"
          Width="150" Height="30">I am bound to be RED!</Button>


    </Grid>
</Window>

还有一个方式不是要datacontext直接在控件上绑定数据源

<DockPanel.Resources>
  <c:MyData x:Key="myDataSource"/>
</DockPanel.Resources>
<Button Width="150" Height="30"
        Background="{Binding Source={StaticResource myDataSource},
                             Path=ColorName}">I am bound to be RED!</Button>

 

本文使用Blog_Backup未注册版本导出,请到soft.pt42.com注册。

posted @ 2010-04-19 00:07  音乐啤酒  阅读(1415)  评论(0)    收藏  举报