Silverlight 在代码后面设置样式不起作用:) 明显的方法不起作用!
我真的需要帮助。我在后面的代码中动态创建一个网格控件,然后将其添加到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该注意,使用
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 specificResourceDictionary
. It does not hunt up the tree of elements looking for the value ofstyle
in otherResourceDictionary
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.