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

简介: 原文:WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新 1、xmlns Mapping URI的格式是 clr-namespace:[;assembly=] (1)如果自定义类和XAML处在同一个Assembly之中,只还需要提供clr-namespace值。
原文: WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新

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"
AI 代码解读

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

xmlns:converter="clr-namespace:Pansoft.SCV.UIFramework.ValueConverter;assembly=Pansoft.SCV.UIFramework"
AI 代码解读

 

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

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

<converter:ImageSourceConverter x:Key="ImageConverter"/>
AI 代码解读


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>
AI 代码解读

后台:

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

或者

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


3、Binding 

前台:

<trans:OpenAccountTranscation x:Key="WorkflowNode"/>
AI 代码解读

或者

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

AI 代码解读

调用:

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


或者直接写:

DataContext = node.Owner.Transcation;
AI 代码解读


 

后台:

            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);  
AI 代码解读


 



4、后台重新绑定

xaml:

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

后台(重新绑定):

BtnSwitchLangs.SetResourceReference(ContentProperty, "ReExecute");//为内容绑定新的源
AI 代码解读


 
目录
打赏
0
0
0
0
216
分享
相关文章
WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树)
原文:WPF 资源(StaticResource 静态资源、DynamicResource 动态资源、添加二进制资源、绑定资源树) 一、WPF对象级(Window对象)资源的定义与查找 实例一: StaticR...
8570 0
WPF技术之Binding
WPF(Windows Presentation Foundation)是微软推出的一种用于创建应用程序用户界面的框架。Binding(绑定)是WPF中的一个重要概念,它用于在界面元素和数据源之间建立关联。通过Binding,可以将界面元素(如文本框、标签、列表等)与数据源(如对象、集合、属性等)进行绑定,从而实现数据的双向传递和同步更新。
349 2
WPF技术之Binding
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
44 0
2000条你应知的WPF小姿势 基础篇<34-39 Unhandled Exceptions和Resource>
WPF-Binding问题-模板样式使用Binding TemplatedParent与TemplateBinding区别
WPF-Binding问题-模板样式使用Binding TemplatedParent与TemplateBinding区别
312 0
WPF中DataGrid控件绑定数据源
WPF中DataGrid控件绑定数据源
200 0
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF界面无法正常显示(资源引用,如转换器),但程序正常运行
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
WPF从外部文件或者程序集加载样式或其他静态资源
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等