WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新

来源:互联网 发布:大众网络报 编辑:程序博客网 时间:2024/06/05 15:51

1、xmlns

Mapping URI的格式是

clr-namespace:<clr namespace>[;assembly=<assembly name>]

(1)如果自定义类和XAML处在同一个Assembly之中,只还需要提供clr-namespace值。
 
xmlns:converter="clr-namespace:Pansoft.SCV.Workflows.OpenAccount.ValueConverter"

(2)如果自定义类和XAML处在不同的Assembly之中。除了clr-namespace值外,还必须提供assembly的值。

xmlns:converter="clr-namespace:Pansoft.SCV.UIFramework.ValueConverter;assembly=Pansoft.SCV.UIFramework"

 

clr-namespace和assembly的拼写必须都是小写。

这样在XAML中就可以通过namespace prefix和类名使用自定义的元素了。举例: 

<converter:ImageSourceConverter x:Key="ImageConverter"/>


2、Resource

前台:

<Page.Resources>        <ResourceDictionary>            <ResourceDictionary.MergedDictionaries>                <ResourceDictionary Source="/Pansoft.SCV.UIFramework;component/Style/GlobaStyle.xaml"/>            </ResourceDictionary.MergedDictionaries>            <converter:ImageSourceConverter x:Key="ImageConverter"/>            <Style TargetType="{x:Type TextBox}">            </Style>        </ResourceDictionary> </Page.Resources>

后台:

            Resources.MergedDictionaries.Add(new ResourceDictionary()            {                Source = new Uri("../Pansoft.SCV.Workflows.OpenAccount;component/Style/GlobaStyle.xaml", UriKind.Relative)            });

或者

Resources.MergedDictionaries.Add(new ResourceDictionary()            {                Source = new Uri("pack://application:,,,/Pansoft.SCV.Workflows.OpenAccount;component/Style/GlobaStyle.xaml")            });


3、Binding 

前台:

<trans:OpenAccountTranscation x:Key="WorkflowNode"/>

或者

Resources.Add("WorkflowNode", node.Owner.Transcation);

调用:

<Page.DataContext>      <Binding Source="{StaticResource WorkflowNode}"/></Page.DataContext>
 
<TextBlock Text="{Binding Name}"  FontSize="18" Margin="20,0"/>


或者直接写:

DataContext = node.Owner.Transcation;


 

后台:

            Binding MyBinding = new Binding();            MyBinding.Path = new PropertyPath("Name");             MyBinding.Mode = BindingMode.OneWay;            MyBinding.Source = node.Owner.Transcation;            MyTextBlock.DataContext = node.Owner.Transcation;            MyTextBlock.SetBinding(TextBlock.TextProperty, MyBinding);  


 



4、后台重新绑定

xaml:

 <Button x:Name="BtnSwitchLangs" Content="{DynamicResource Execute}" Width="200" Height="60" Click="Button_Click_2" Margin="0,5"/>

后台(重新绑定):

BtnSwitchLangs.SetResourceReference(ContentProperty, "ReExecute");//为内容绑定新的源


 

原创粉丝点击