c# - 将附加属性绑定到控件模板内的附加属性
问题描述
这是我的问题的一个工作示例: https ://github.com/themimcompany/AttachedPropertyIssue
我有一个定义 a的Style
for a 。在里面我有一个具有相同附加属性的。我想将 ' 的附加属性设置/绑定到' 的附加属性的值。附加属性是一个简单的值。Button
ControlTemplate
ControlTemplate
TextBlock
TextBlock
Button
int
我如何让这个工作?这可以在 UWP 中完成吗?我收到的错误没有告诉我如何解决这个问题。像Unspecified error
...
这是我的风格:
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<TextBlock local:Translation.TranslationModelID="{Binding Path=(local:Translation.TranslationModelID),
RelativeSource={RelativeSource TemplatedParent}}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
这是我的附加属性定义:
public class Translation
{
public static int GetTranslationModelID(DependencyObject obj)
{
return (int)obj.GetValue(TranslationModelIDProperty);
}
public static void SetTranslationModelID(DependencyObject obj, int value)
{
obj.SetValue(TranslationModelIDProperty, value);
}
public static readonly DependencyProperty TranslationModelIDProperty =
DependencyProperty.RegisterAttached("TranslationModelID", typeof(int), typeof(FrameworkElement), new PropertyMetadata(0));
}
这是我要定义的按钮,请注意我要分配按钮的附加属性 - 然后在 ControlTemplate 中获取值并将其分配给 TextBlock 的附加属性(查看样式):
<Button local:Translation.TranslationModelID="1" />
再次解释一下:我有一个附加属性分配给 a Button
,我想将 that 的附加属性的值分配给 that的a的Button
相同附加属性。它没有按我的预期工作。我在运行时遇到异常。我如何让这个工作?TextBlock
ControlTemplate
Button
Style
Unspecified error..
这是我的问题的一个工作示例: https ://github.com/themimcompany/AttachedPropertyIssue
编辑:
这种方法给了我一个不同的错误。The property 'TranslationModelID' was not found in type 'PassingAttachedPropertyInControlTemplater.Translation'. [Line: 17 Position: 40]
<Page.Resources>
<Style TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Grid>
<TextBlock local:Translation.TranslationModelID="{TemplateBinding local:Translation.TranslationModelID}" />
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</Page.Resources>
<Grid>
<Button x:Name="btn" local:Translation.TranslationModelID="1" />
</Grid>
解决方案
我想使用 TemplateBinding 或常规绑定将模板化父级的附加属性分配给 ControlTemplate 内元素的附加属性
根据您的要求,您可以创建继承按钮的模板化控件。然后在后面的代码中设置 TextBlock Attached 属性。请检查以下段代码。
public sealed class CustomButton : Button, IDisposable
{
private long token;
private TextBlock Tbk;
public CustomButton()
{
this.DefaultStyleKey = typeof(CustomButton);
}
private void CallBackMethod(DependencyObject sender, DependencyProperty dp)
{
UpdateAttachedPropertyValue();
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
token = this.RegisterPropertyChangedCallback(AttachedPropertyTest.Translation.TranslationModelIDProperty, CallBackMethod);
Tbk = GetTemplateChild("TBK") as TextBlock;
UpdateAttachedPropertyValue();
}
public void Dispose()
{
this.UnregisterPropertyChangedCallback(AttachedPropertyTest.Translation.TranslationModelIDProperty,token);
}
private void UpdateAttachedPropertyValue()
{
AttachedPropertyTest.Translation.SetTranslationModelID(Tbk, AttachedPropertyTest.Translation.GetTranslationModelID(this));
}
}
Xaml
<Style TargetType="local:CustomButton" >
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomButton">
<Border
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<Grid>
<TextBlock x:Name="TBK"/>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
推荐阅读
- android - 如何更改启动器 3 文件夹图标形状以绘制松鼠形状?
- html - 使用 css 显示 3 个带有标题的图形
- php - Neo4j - Graphaware reco4php - 命名空间问题
- sql - SQL Server 循环遍历行以形成组
- bash - 每一行的 awk exec 命令并保留列
- sql - SQL中联合去重问题如何解决
- javascript - Webpack 4 不排除 node_modules
- apache-nifi - Apache-NiFi DetectDuplicate 处理器计数器
- javascript - 在自动生成的表中获取一个特定的行
- python - 为什么分配给 __class__ 单元格会破坏“超级”?