gpt4 book ai didi

wpf - 为用户控件设置样式

转载 作者:行者123 更新时间:2023-12-01 01:42:13 30 4
gpt4 key购买 nike

我正在尝试为我的用户控件设置样式。 UserControl 位于项目“Controls”中,主题位于项目“MainProject”中

<UserControl x:Class="Controls.OutputPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
mc:Ignorable="d"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Name="OutputControl">
<!-- Style="{DynamicResource UserControlStyle}"> - I cant set the style here because the Resource Dictionary hasn't been defined yet -->

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MainProject;component/Themes/MyTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>

<!-- Now that the Resource Dictionary has been defined I need to set the style -->

<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>

<TextBox x:Name="textbox"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Text="{Binding ElementName=OutputControl, Path=TextProperty}"
IsReadOnly="True"
Style="{DynamicResource OutputTextBoxStyle}"/>

</Grid>

</UserControl>

最佳答案

据我所知,这应该可以正常工作。您是否收到任何特殊警告或错误,或者样式中的某些部分没有被应用?

Resources设置后设置样式,可以使用如下语法

<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/MainProject;component/Themes/MyTheme.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
<UserControl.Style>
<DynamicResource ResourceKey="UserControlStyle"/>
</UserControl.Style>

如果您在此之后仍然遇到问题,可以将其与我在此处上传的示例应用程序进行比较:http://www.mediafire.com/?q1v98huubzw02zb

关于wpf - 为用户控件设置样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5154161/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com