托管网站服务器,WordPress如何加代码,企业官网的应用场景,长春网站开发推荐学习笔记#xff1a;
1. 使用{Binding}标记拓展在XAML中进行绑定。通过绑定#xff0c;所有数据上下文的变化都将自动更新到控件上。
TextBlock Text{Binding Name}/ 2. 绑定模式
可以通过指定{Binding}的Mode来修改绑定的行为: OneWay 源的变化自动…学习笔记
1. 使用{Binding}标记拓展在XAML中进行绑定。通过绑定所有数据上下文的变化都将自动更新到控件上。
TextBlock Text{Binding Name}/ 2. 绑定模式
可以通过指定{Binding}的Mode来修改绑定的行为: OneWay 源的变化自动传播到目标 TwoWay 源和目标的变化互相传播 OneTime 源的初始值会应用到目标但后续的变化会被无视 OneWayToSource 目标的变化自动传播到源 Default 依据属性而定
3.绑定变化通知
private int _selectedParityIndex;
public int SelectParityIndex
{get _selectedParityIndex;set this.RaiseAndSetIfChanged(ref _selectedParityIndex, value);
}
4。与控件绑定
TextBox Nametb1!-- 绑定到命名为“tb1”控件的Text属性 --
TextBlock Text{Binding #tb1.Text}/
5. 绑定到父级
Border TagHello Avalonia!TextBlock Text{Binding $parent.Tag}/
/Border!-- 绑定到祖先级 --
Border TagHello Avalonia!BorderTextBlock Text{Binding $parent[1].Tag}//Border
/Border
!-- 按类型绑定到祖先 --
Border TagHello Avalonia!DecoratorTextBlock Text{Binding $parent[Border].Tag}//Decorator
/Border
6. 绑定到转换器
Window.Resourceslocal:MyConverter x:Keyconverter1/
/Window.ResourcesTextBlock Text{Binding Value, Converter{StaticResource converter1}}/
内置转换器
转换器描述 StringConverters.IsNullOrEmpty 如果输入的字符串为null或empty则返回true StringConverters.IsNotNullOrEmpty 如果输入的字符串不为null或empty则返回true ObjectConverters.IsNull 如果输入为null则返回true ObjectConverters.IsNotNull 如果输入为null则返回false BoolConverters.And 一种多值转换器如果所有输入均为true则返回true。 BoolConverters.Or 一种多值转换器如果其中任意输入为true则返回true。
可以作为管道使用也可以作为控制控件的现实和隐藏。
!-- 控制是否隐藏 --
TextBlock Text{Binding MyText}IsVisible{Binding MyText, Converter{x:Static StringConverters.IsNotNullOrEmpty}}/
!-- 转换大小写 --
TextBlock Text{Binding TheContent, Converter{StaticResource textCaseConverter},ConverterParameterlower} /
7. 绑定到命令
Button Command{Binding DoTheThing}Do the thing!/Button
public ReactiveCommandUnit, Unit DoTheThing { get; }
public MainWindowViewModel()
{DoTheThing ReactiveCommand.Create(RunTheThing);
}void RunTheThing()
{// 此处执行命令的代码。
}
8. 绑定到方法
public void RunTheThing(string parameter)
{// 此处执行命令的代码。
}
可用CommandParameter传递参数。
如果需要从viewmodel里面触发CanExcute则必须用一个或多个DependsOn属性来修饰它。
[DependsOn(nameof(IsTheThingEnabled))]
bool CanRunTheThing(/* CommandParameter */object parameter)
{return IsTheThingEnabled parameter ! null;
}
9.绑定到任务
public Taskstring MyAsyncText GetTextAsync();private async Taskstring GetTextAsync()
{await Task.Delay(1000);return Hello from async operation;
}
TextBlock Text{Binding MyAsyncText^, FallbackValueWait a second} /
10.订阅属性的更改
var textBlock new TextBlock();
var text textBlock.GetObservable(TextBlock.TextProperty);
text.Subscribe(value Console.WriteLine(value Changed));
11.绑定到可观察对象
// 我们在这里使用Rx Subject以便我们可以使用OnNext推送新值
var source new Subjectstring();
var textBlock new TextBlock();// 将TextBlock.Text绑定到源
var subscription textBlock.Bind(TextBlock.TextProperty, source);// 将textBlock.Text设置为“hello”
source.OnNext(hello);
// 将textBlock.Text设置为“world!”
source.OnNext(world!);// 终止绑定
subscription.Dispose();
12.控件模板中绑定
TextBlock Nametb Text{TemplateBinding Caption}/!-- 也能写成这样 --
TextBlock Nametb Text{Binding Caption, RelativeSource{RelativeSource TemplatedParent}}/
TemplateBinding只接受单个属性而不是属性路径因此如果要使用属性路径进行绑定则必须使用第二种语法
!-- 这不起作用因为TemplateBinding只接受单个属性 --
TextBlock Nametb Text{TemplateBinding Caption.Length}/!-- 在这种情况下必须使用此语法 --
TextBlock Nametb Text{Binding Caption.Length, RelativeSource{RelativeSource TemplatedParent}}/
TemplateBinding仅支持单向绑定(OneWay) 、只能用于IStyledElement
!-- 这不起作用因为GeometryDrawing不是IStyledElement --
GeometryDrawing Brush{TemplateBinding Foreground}/!-- 在这种情况下必须使用此语法 --
GeometryDrawing Brush{Binding Foreground, RelativeSource{RelativeSource TemplatedParent}}/