使用 TemplateBinding 更新源
我对所有标签
<Style TargetType="Label" x:Key="LabelStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<StackPanel Orientation="Horizontal" >
<TextBox Loaded="MyTextBlock_Loaded" x:Name="EditControl" Visibility="Collapsed" Text="{TemplateBinding Tag}" />
<Label Content="{TemplateBinding Content}" Grid.Column="1" Grid.Row="1">
</Label>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
和示例标签
<Label Grid.Column="0" Grid.Row="0" Content="Photo" Style="{StaticResource LabelStyle}" Tag="{Binding fieldsCode.firstName, UpdateSourceTrigger=PropertyChanged}"/>
都使用这种样式,但我觉得 TemplateBiding 不支持属性更新。如何解决这个问题
I use this style for all my labels
<Style TargetType="Label" x:Key="LabelStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Label">
<StackPanel Orientation="Horizontal" >
<TextBox Loaded="MyTextBlock_Loaded" x:Name="EditControl" Visibility="Collapsed" Text="{TemplateBinding Tag}" />
<Label Content="{TemplateBinding Content}" Grid.Column="1" Grid.Row="1">
</Label>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
and my sample label
<Label Grid.Column="0" Grid.Row="0" Content="Photo" Style="{StaticResource LabelStyle}" Tag="{Binding fieldsCode.firstName, UpdateSourceTrigger=PropertyChanged}"/>
But I feel that TemplateBiding doesn't support update of property. How can solve this issue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试双向绑定
Try this for two-way binding
如果您希望从 ControlTemplate 内部到其模板化父级的属性进行单向绑定,请使用 {TemplateBinding}。对于所有其他场景,请改用 {Binding}:
If you want a one-way binding from within the ControlTemplate to a property of its templated parent, use {TemplateBinding}. For all other scenarios use {Binding} instead:
<TextBox Loaded="MyTextBlock_Loaded" x:Name="EditControl" Visibility="Collapsed" Text="{Binding Tag, Mode=TwoWay}" />