c# - 在代码中绑定到可绑定的附加属性
问题描述
来自官方文档:
然后在特定控件上设置附加属性时使用命名空间声明,如以下 XAML 代码示例所示:
<Label Text="Label Shadow Effect" local:ShadowEffect.HasShadow="true" />
等效的 C# 代码显示在以下代码示例中:
var label = new Label { Text = "Label Shadow Effect" }; ShadowEffect.SetHasShadow (label, true);
我的问题:
这将是以下 XAML 绑定的等效 C# 代码:
<Label Text="Label Shadow Effect" local:ShadowEffect.HasShadow="{Binding HasShadow}" />
示例场景
我有一个LongPressEffect
公开附加的 Bindable Property Command
。效果附在a上Label
,如下图:
<Label x:Name="LongPressLabel"
Text="Long press me"
effects:LongPressEffect.Command = "{Binding MyCommand}">
<Label.Effects>
<effects:LongPressEffect />
</Label.Effects>
</Label>
如何在 C# 代码上执行相同的绑定?
相似主题
似乎与此处提出的问题相同。有任何想法吗?
解决方案
According to this document, you can try the following code:
label1.Effects.Add(new LongPressedEffect());
LongPressedEffect.SetCommand(label1,mycommand);
The equivalent Label in C#
<Label
x:Name="label1"
effects:LongPressedEffect.Command="{Binding command1}"
effects:LongPressedEffect.CommandParameter="{Binding .}"
BackgroundColor="Red"
Text="Long Press Me!">
<Label.Effects>
<effects:LongPressedEffect />
</Label.Effects>
</Label>
推荐阅读
- excel - 我只想看到实际的 x 值显示在 Excel 图表的水平轴上(比例与这些值匹配)不让 Excel 进行缩放和标记
- html - 选项卡的 onclick 选项卡内容不完全可见(jquery)
- typescript - 在 vue 3 中从另一个本地项目导入和显示组件
- google-sheets - 谷歌表 - 简单的 importxml 公式不起作用
- scala - SPARK/SCALA 中 oracle RAWTOHEX/HEXTORAW 的等效 API 是什么?
- vb.net - 如果语句没有捕获捕获条件
- php - 为什么我没有从控制器的视图中获取表名?
- css - 在 react.js 中选择时更改字体/按钮的重量
- java - 将 lambda (Object -> Object) 传递给 Java 中的函数
- javascript - 加载嵌入式 css 和图像以供未经授权的用户访问时出现问题