Silverlight 在代码后面设置样式不起作用:) 明显的方法不起作用!

发布于 2024-11-26 14:39:09 字数 1022 浏览 2 评论 0原文

我真的需要帮助。我在后面的代码中动态创建一个网格控件,然后将其添加到 xaml 中定义的包含控件的子级中。现在,一切都按预期动态创建,但不幸的是,当我以相同的方式设置样式时,我设置了添加到网格中的文本框的文本并相应地在行/列中定位,但它不起作用。请注意以下代码:

        AddTextBlock(7, col, String.Format("{0:0}%", finances.PrivateDaysPercent), "GridValueStyle");


    TextBlock AddTextBlock( int row, int column, string text, string style)
    {
        Style s = Resources[style] as Style;
        TextBlock tb = new TextBlock() { Text = text};
        tb.Style = s;
        Grid.SetColumn(tb, column);
        Grid.SetRow(tb, row);
        grid.Children.Add(tb);
        return tb;
    }

    <Style x:Key="GridValueStyle" TargetType="TextBlock" BasedOn="{StaticResource ContentTextStyle}" >
        <Setter Property="Margin" Value="2,1" />
        <Setter Property="HorizontalAlignment" Value="Right"/>
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

显然应该设置样式,但事实并非如此。该样式在资源字典中正确定义并添加到 app.xaml 中。我知道它有效,因为我在另一个导航页面中使用了这种样式,并且它完美地适用于 xaml 中静态创建的网格。

I really need help. I am dynamically creating a grid control in my code behind, then adding it to the children of the containing control that was defined in the xaml. Now, everything is dynamically being created as expected, but unfortunately when I set the style in the same way I set the text of the textboxes which I add to the grid and position in row/columns accordingly it doens't work. Notice the following code:

        AddTextBlock(7, col, String.Format("{0:0}%", finances.PrivateDaysPercent), "GridValueStyle");


    TextBlock AddTextBlock( int row, int column, string text, string style)
    {
        Style s = Resources[style] as Style;
        TextBlock tb = new TextBlock() { Text = text};
        tb.Style = s;
        Grid.SetColumn(tb, column);
        Grid.SetRow(tb, row);
        grid.Children.Add(tb);
        return tb;
    }

    <Style x:Key="GridValueStyle" TargetType="TextBlock" BasedOn="{StaticResource ContentTextStyle}" >
        <Setter Property="Margin" Value="2,1" />
        <Setter Property="HorizontalAlignment" Value="Right"/>
        <Setter Property="VerticalAlignment" Value="Center" />
    </Style>

The style should clearly be setting, but it isn't. The style is defined correctly in the resource dictionary and added to app.xaml. I know it works becuase I use this style in another navigation page and it applies perfectly to a statically created grid in xaml.

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

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

发布评论

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

评论(1

不交电费瞎发啥光 2024-12-03 14:39:09

您应该注意,使用Resources[style]尝试从该特定ResourceDictionary检索密钥。它不会在元素树中寻找其他 ResourceDictionary 对象中的 style 值。这常常让开发人员感到惊讶,因为这就是在 xaml 中使用 {StaticResource ....} 时发生的情况。

我猜想您包含的代码位于 UserControl 中,因此要找到“GridValueStyle”,它必须专门位于 UserControl 的资源中。如果它在子资源中(例如常见的“LayoutRoot”的 ),则不会找到它,如果是在子资源中,也不会找到它在 App.Xaml 中。

You should note that using Resources[style] only attempts to retrieve the key from that specific ResourceDictionary. It does not hunt up the tree of elements looking for the value of style in other ResourceDictionary objects. This often takes devs by surprise since that is what happens when {StaticResource ....} is used in xaml.

I would guess the code you have included is in a UserControl hence for "GridValueStyle" to be found it must be specifically in the UserControl's Resources. If it's in the resources of a child (such as the <Grid.Resources> of the "LayoutRoot" which is common) then it won't be found, nor will it be found if it is in the App.Xaml.

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