【问题标题】:Apply Style From Resource to ListView.ItemContainerStyle将样式从资源应用到 ListView.ItemContainerStyle
【发布时间】:2014-02-10 13:35:47
【问题描述】:

我在 VS 2012 中使用 XAML/WPF。我承认我还不太了解模板和样式。

我在 application.xaml 文件中定义了一个样式,如下所示:

<Style x:Key="ContactGroups" TargetType="ListViewItem">
    <!-- Styling omitted here -->
</Style>

现在我想将此样式应用于我的列表视图,但我无法确定在何处应用此样式,即在何处放置代码以设置样式。我在这里省略了很多属性以保持简短:

<ListView ItemsSource="{Binding Groups}" SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}" >
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <WrapPanel />
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid Width="140" Height="25">
                <Grid.RowDefinitions>
                    <RowDefinition Height="2*" />
                </Grid.RowDefinitions>
                <Label Content="{Binding Name}" ToolTip="{Binding Name}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    使用StaticResource 标记扩展在ListBox 的ItemContainerStyle 上设置样式:

    <ListView ItemsSource="{Binding Groups}"
              SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}"
              ItemContainerStyle="{StaticResource ContactGroups}" >
    

    【讨论】:

    • 运行和编译没有错误,但我的样式没有得到应用。我真的很困惑这怎么可能。当获取正在使用的颜色的十六进制并在我的 XAML(或整个应用程序)中搜索它们时,颜色不会出现在任何地方。我怀疑你的回答是正确的,我还有其他问题。
    • 是的,您发布的问题。这就是你实现它的方式。不确定您的其他问题。
    • 考虑打开另一个问题,其中包含您的风格的详细信息,以便更多人看到它。
    • 我认为这可能是 Win 8.x 的错误。查看我的新帖子:stackoverflow.com/questions/21686509/…
    【解决方案2】:

    我不确定您是否只想将此样式应用于此列表,但如果不是,您可以简单地从样式中删除 x:Key="ContactGroups",它应该应用于所有列表项。

    如果您只想定位此列表视图,则可以选择将样式添加到列表视图的资源中:

        <ListView ItemsSource="{Binding Groups}" SelectedItem="{Binding Path=SelectedGroup, Mode=OneWayToSource}" >
            <ListView.Resources>
                <Style TargetType="ListViewItem">
                    <!-- Styling omitted here -->
                </Style>
            </ListView.Resources>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid Width="140" Height="25">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="2*" />
                        </Grid.RowDefinitions>
                        <Label Content="{Binding Name}" ToolTip="{Binding Name}" />
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    

    编辑:根据您在下面的评论,这可能是您想要采取的方法:

    <ListView.Resources>
        <Style TargetType="ListViewItem" BasedOn="{StaticResource ContactGroups}" />
    </ListView.Resources>
    

    这样,您的样式将保持在 App.xaml 中定义。

    【讨论】:

    • 谢谢,我认为这实际上可能为我指明了正确的方向。我想知道我是否有另一种“全局”样式应用于我的列表,导致我在上面的 cmets 中提到的问题。感谢您对使用本地资源的建议。实际上,我现在正试图将我的样式移出 xaml 窗口,因为该窗口变得过于复杂且难以阅读。
    • Oic。在这种情况下,我还有一个您可能更喜欢的选择。我会在上面添加它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多