【发布时间】:2019-01-13 00:24:29
【问题描述】:
我有一个这样的用户控件:
<UserControl>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<HyperlinkButton Grid.Row="0" />
<TextBlock Name="textblock" Grid.Row="1"
Text="{Binding dailyText, ElementName=userControl}">
</TextBlock>
</Grid>
</UserControl>
不过,我不知道,如何设置从主窗口到用户控件的样式?我已经解决了访问其他属性的问题,如下所示:
public static readonly DependencyProperty MyContentProperty =
DependencyProperty.Register("MyContent", typeof(object), typeof(Day), null);
public object MyContent
{
get { return (object)GetValue(MyContentProperty ); }
set { SetValue(MyContentProperty , value); }
}
然后
<local:Day MyContent="Hello World" />
但是,它不适合这种风格。风格没有变化。
谢谢。
(修改)
下面是一个 mainWindow 部分。
<Page.Resources>
<Style TargetType="TextBlock" x:Name="MyTextBlockStyle">
<Setter Property="Foreground" Value="Blue" />
<Setter Property="SelectionHighlightColor" Value="Red"/>
<Setter Property="FontSize" Value="10"/>
</Style>
</Page.Resources>
<local:Day MyStyle="{StaticResource MyTextBlockStyle}">
userControl 中的代码后面部分
public static readonly DependencyProperty MyStyleProperty =
DependencyProperty.Register("MyStyle", typeof(Style), typeof(Day), null);
public Style MyStyle
{
get { return (Style)GetValue(MyStyleProperty); }
set { SetValue(MyStyleProperty, value); }
}
【问题讨论】:
-
SO 不是免费的代码交付网站。您尝试过什么需要用代码解释?请参考这个stackoverflow.com/help/how-to-ask
-
感谢您纠正我的错误。此修改符合您的意图吗?
-
显示“主窗口中的样式”部分
标签: xaml uwp user-controls styles uwp-xaml