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

1、xmlns

Mapping URI的格式是

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

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

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

[html]   view plain copy
  1. xmlns:converter="clr-namespace:Pansoft.SCV.UIFramework.ValueConverter;assembly=Pansoft.SCV.UIFramework"  

 

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

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

[html]   view plain copy
  1. <converter:ImageSourceConverter x:Key="ImageConverter"/>  


2、Resource

前台:

[html]   view plain copy
  1. <Page.Resources>  
  2.         <ResourceDictionary>  
  3.             <ResourceDictionary.MergedDictionaries>  
  4.                 <ResourceDictionary Source="/Pansoft.SCV.UIFramework;component/Style/GlobaStyle.xaml"/>  
  5.             </ResourceDictionary.MergedDictionaries>  
  6.             <converter:ImageSourceConverter x:Key="ImageConverter"/>  
  7.             <Style TargetType="{x:Type TextBox}">  
  8.             </Style>  
  9.         </ResourceDictionary>  
  10.  </Page.Resources>  

后台:

[csharp]   view plain copy
  1. Resources.MergedDictionaries.Add(new ResourceDictionary()  
  2. {  
  3.     Source = new Uri("../Pansoft.SCV.Workflows.OpenAccount;component/Style/GlobaStyle.xaml", UriKind.Relative)  
  4. });  

或者

[csharp]   view plain copy
  1. Resources.MergedDictionaries.Add(new ResourceDictionary()  
  2.             {  
  3.                 Source = new Uri("pack://application:,,,/Pansoft.SCV.Workflows.OpenAccount;component/Style/GlobaStyle.xaml")  
  4.             });  


3、Binding 

前台:

[html]   view plain copy
  1. <trans:OpenAccountTranscation x:Key="WorkflowNode"/>  

或者

[csharp]   view plain copy
  1. Resources.Add("WorkflowNode", node.Owner.Transcation);  

调用:

[html]   view plain copy
  1. <Page.DataContext>  
  2.       <Binding Source="{StaticResource WorkflowNode}"/>  
  3. </Page.DataContext>  
[html]   view plain copy
  1.    
[html]   view plain copy
  1. <TextBlock Text="{Binding Name}"  FontSize="18" Margin="20,0"/>  


或者直接写:

[csharp]   view plain copy
  1. DataContext = node.Owner.Transcation;  


 

后台:

[csharp]   view plain copy
  1. Binding MyBinding = new Binding();  
  2. MyBinding.Path = new PropertyPath("Name");   
  3. MyBinding.Mode = BindingMode.OneWay;  
  4. MyBinding.Source = node.Owner.Transcation;  
  5. MyTextBlock.DataContext = node.Owner.Transcation;  
  6. MyTextBlock.SetBinding(TextBlock.TextProperty, MyBinding);    


 



4、后台重新绑定

xaml:

[html]   view plain copy
  1. <Button x:Name="BtnSwitchLangs" Content="{DynamicResource Execute}" Width="200" Height="60" Click="Button_Click_2" Margin="0,5"/>  

后台(重新绑定):

[csharp]   view plain copy
  1. BtnSwitchLangs.SetResourceReference(ContentProperty, "ReExecute");//为内容绑定新的源  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值