我最近解决了一个样式问题,在堆栈溢出处有一些帮助......(你可以在我的历史中看到问题/答案)
因此,我想出了以下样式来应用于特定的文本框。
<!--Expanding text box with the max width in the tag property-->
<!---->
<!--Will not work with password box!!-->
<Style TargetType="{x:Type TextBox}"
x:Key="ExpandingTextBoxMaxWidthInTag">
<Setter Property="OverridesDefaultStyle"
Value="True" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TextBox">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*">
<ColumnDefinition.MaxWidth>
<Binding Path="Tag"
RelativeSource="{RelativeSource TemplatedParent}">
</Binding>
</ColumnDefinition.MaxWidth>
</ColumnDefinition>
<ColumnDefinition Width="0*" />
</Grid.ColumnDefinitions>
<TextBox>
<TextBox.Text>
<Binding Path="Text"
RelativeSource="{RelativeSource TemplatedParent}" />
</TextBox.Text>
<TextBox.MaxWidth>
<Binding Path="Tag"
RelativeSource="{RelativeSource TemplatedParent}" />
</TextBox.MaxWidth>
</TextBox>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
(代码清单1)
然后我将其应用于我的代码中的文本框,如此...
<TextBox Tag="150"
Text="{Binding Path=Username}"
Style="{StaticResource ExpandingTextBoxMaxWidthInTag}" />
(代码清单2)
我必须强调,正如所写,这段代码可以按照需要运行。问题出现的时候,我将 UpdateSourceTrigger = PropertyChanged 添加到 文本 属性的绑定...
<TextBox Tag="150"
Text="{Binding Path=Username, UpdateSourceTrigger=PropertyChanged}"
Style="{StaticResource ExpandingTextBoxMaxWidthInTag}" />
(代码清单3)
清单3中的代码有一个例外:更新源触发器无法正确触发。它不会在每次按键上触发(就像没有应用样式一样),而只会触发丢失的焦点事件(默认值)。
我不能简单地在样式中明确应用“PropertyChanged”,因为我需要将样式应用于一堆对更新源触发器有不同需求的TextBox。
如何在我的xaml中获取指定的更新源触发器以过滤我的样式?
答案 0 :(得分:0)
您用您的样式覆盖“Text”属性,因此还有“UpdateSourceTrigger = PropertyChanged”部分,不是吗?只需删除你的Style中的“Text”属性绑定,这应该可以正常工作。