在运行时从代码隐藏设置样式设置器值

发布于 2024-12-22 11:38:25 字数 653 浏览 1 评论 0 原文

在 silverlight 中,存在一个已知的文本框插入符错误,此处讨论:http://forums。 silverlight.net/p/165276/423268.aspx

作为解决方法,使用附加行为允许显式指定 TextBox 插入符号的颜色。

因此,我的 TextBox 样式有以下设置器:

<Style x:Key="NameEditStyle" TargetType="TextBox">
            <Setter Property="Utilities:FixCaretBrushBehavior.CaretBrush" Value="White" />

我的应用程序在 Windows Phone 上运行,其中 TextBox 可以有白色和黑色背景。我需要有条件地修改插入符号是否显示为白色到黑色。 (这相当于设置属性的Value)。

如何从代码中有条件地更改样式设置器中的此特定属性?

我尝试为属性提供 x:Name 并尝试在代码隐藏中引用它,但该属性始终为 null,因此我无法调整它的值。

In silverlight, there is a known textbox caret bug that is discussed here: http://forums.silverlight.net/p/165276/423268.aspx

As a workaround, an attached behaviour is used that allows to specify the color for TextBox caret explicitly.

Therefore I have the following setter in my TextBox style:

<Style x:Key="NameEditStyle" TargetType="TextBox">
            <Setter Property="Utilities:FixCaretBrushBehavior.CaretBrush" Value="White" />

My application is running on Windows Phone, where there can be both White and Black backgrounds for the TextBoxes. I need to conditionally modify whether the caret will appear White to Black. (which is equivalent to setting a Value of the property).

How can I conditionally change this particular property in a style setter from code?

I have tried giving a property an x:Name and trying to reference it in code-behind, but the property is always null, so I can't adjust it's value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福%小乖 2024-12-29 11:38:25

WPF/SL/WP7 无法在加载后更改样式,因为 Style.IsSealed 将为 true。
您可以做的是在旧样式的基础上创建新样式,并将 TextBoxes 样式更改为新样式:

<Style x:Key="NameEditStyle" TargetType="TextBox">
    <Setter Property="Utilities:FixCaretBrushBehavior.CaretBrush" Value="White" />
    ...
</Style>

<Style x:Key="BlackNameEditStyle" TargetType="TextBox" BasedOn="{StaticResource NameEditStyle}">
    <Setter Property="Utilities:FixCaretBrushBehavior.CaretBrush" Value="Black" />
</Style>

或者,您也可以在运行时创建此新样式,如 文章

There is no way in WPF/SL/WP7 to change a style after it is loaded because the Style.IsSealed will be true.
What you can do is to create new style based on the old one and change the TextBoxes style to the new style:

<Style x:Key="NameEditStyle" TargetType="TextBox">
    <Setter Property="Utilities:FixCaretBrushBehavior.CaretBrush" Value="White" />
    ...
</Style>

<Style x:Key="BlackNameEditStyle" TargetType="TextBox" BasedOn="{StaticResource NameEditStyle}">
    <Setter Property="Utilities:FixCaretBrushBehavior.CaretBrush" Value="Black" />
</Style>

Alternatively you can also create this new style during runtime as described here in the article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文