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");//为内容绑定新的源
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");//为内容绑定新的源
相关文章推荐
- WPF笔记 ( xmlns引用,Resource、Binding 前/后台加载,重新绑定) 2013.6.7更新
- Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题
- wpf 后台绑定数据 binding
- 重新想象 Windows 8 Store Apps (54) - 绑定: 增量方式加载数据
- WPF数据绑定(2 绑定列表数据Binding to List Data)
- 根据已经加载好的datagrid数据,通过条件查询进行重新根据查询的条件进行加载:(无需通过后台再次查询)
- RDLC报表更新和重新绑定数据源引用
- Wpf 数据绑定之BindingBase.StringFormat
- WPF学习笔记-Expression Blend中的绑定(一)
- WPF数据绑定Binding(二)
- WPF and Silverlight 学习笔记(二十二):使用代码实现绑定、绑定数据的验证
- Google Data Binding Library 谷歌官方数据绑定库(三)在引用布局中使用数据绑定
- WPF学习笔记:(一)数据绑定与DataContext
- WPF学习笔记一 依赖属性及其数据绑定
- WPF/Silverlight2.0 Binding(数据绑定)机制详解
- c#--wpf数据绑定binding
- WPF 学习笔记 - 11. Binding (4)
- WPF笔记--Binding
- 【转载】wpf学习笔记数据绑定9
- WPF 如何在后台进行控件属性的绑定MVVM