【问题标题】:Styles overwriting in WPFWPF 中的样式覆盖
【发布时间】:2012-05-07 21:27:38
【问题描述】:

我正在使用MahApps.Metro 在我的应用中实现 Metro UI。

我有一个列表视图,MahApps.Metro 正在改变它的样式。列表视图的 MahApps 样式为 here

加载样式:

<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Colours.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Fonts.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.AnimatedSingleRowTabControl.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/Blue.xaml" />
            <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Accents/BaseLight.xaml" />
            <ResourceDictionary Source="pack://application:,,,/FineRSS;component/Resources/Icons.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>

我需要跟踪选定的列表视图项,所以我使用了下一个方法:

<ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/>
                </Style>
</ListView.ItemContainerStyle>

但是 MahApps.Metro 的样式被覆盖为 ListView 的默认样式。

我可以做些什么来保持样式和 IsSelected 绑定?

【问题讨论】:

    标签: wpf xaml styles


    【解决方案1】:

    我不确定我是否遵循您的操作,但是将您的 Style 设置为默认加载的 BasedOn 是否有意义?

    类似

    <ListView.ItemContainerStyle> 
        <Style TargetType="{x:Type ListViewItem}" 
               BasedOn="{StaticResource {x:Type ListViewItem}}"> 
            <Setter Property="IsSelected" Value="{Binding Mode=TwoWay, Path=IsSelected}"/> 
        </Style> 
    </ListView.ItemContainerStyle> 
    

    【讨论】:

    • 虽然 VS 不喜欢 BasedOn="{StaticResource {x:Type ListViewItem}}" 行,但它似乎正在工作。谢谢。
    • 我有同样的问题。仍然习惯于使用 XAML。
    猜你喜欢
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2019-06-04
    相关资源
    最近更新 更多