您的位置:首页 > 其它

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

2014-01-13 20:15 302 查看
1、xmlns

Mapping URI的格式是

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

(1)如果自定义类和XAML处在同一个Assembly之中,只还需要提供clr-namespace值。
 

[html] view
plaincopy

xmlns:converter="clr-namespace:Pansoft.SCV.Workflows.OpenAccount.ValueConverter"  

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

[html] view
plaincopy

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

 

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

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

[html] view
plaincopy

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

2、Resource

前台:

[html] view
plaincopy

<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>  

后台:

[csharp] view
plaincopy

Resources.MergedDictionaries.Add(new ResourceDictionary()  

{  

    Source = new Uri("../Pansoft.SCV.Workflows.OpenAccount;component/Style/GlobaStyle.xaml", UriKind.Relative)  

});  

或者

[csharp] view
plaincopy

Resources.MergedDictionaries.Add(new ResourceDictionary()  

            {  

                Source = new Uri("pack://application:,,,/Pansoft.SCV.Workflows.OpenAccount;component/Style/GlobaStyle.xaml")  

            });  

3、Binding 

前台:

[html] view
plaincopy

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

或者

[csharp] view
plaincopy

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

调用:

[html] view
plaincopy

<Page.DataContext>  

      <Binding Source="{StaticResource WorkflowNode}"/>  

</Page.DataContext>  

[html] view
plaincopy

   

[html] view
plaincopy

<TextBlock Text="{Binding Name}"  FontSize="18" Margin="20,0"/>  

或者直接写:

[csharp] view
plaincopy

DataContext = node.Owner.Transcation;  

 

后台:

[csharp] view
plaincopy

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:

[html] view
plaincopy

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

后台(重新绑定):

[csharp] view
plaincopy

BtnSwitchLangs.SetResourceReference(ContentProperty, "ReExecute");//为内容绑定新的源  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: