简体   繁体   English

使用主题时的数据模板不起作用-WPF

[英]Datatemplates while using theme does not work - WPF

I am using the theme DarkExpression from WPF Futures . 我正在使用WPF Futures的主题DarkExpression。 It does not seem to work well with datatemplates. 它似乎不适用于数据模板。

Scenario 1: 方案1:

Here is how it looks like without datatemplates: 没有数据模板的情况如下:

在此处输入图片说明

Code: 码:

<ListView Name="playlistListView"  ItemsSource="{Binding PlaylistList}" Margin="0" SelectionChanged="DatabindedPlaylistListView_SelectionChanged" Background="{x:Null}" Opacity="0.98">
        <ListView.View>
            <GridView>
                <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Name}">
                    <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Playlist" Tag="Playlist"/>
                </GridViewColumn>
            </GridView>
        </ListView.View>
</ListView>

Scenario 2: Here is how it looks like trying to use datatemplates while using the theme: 方案2:这是在使用主题时尝试使用数据模板的样子:

在此处输入图片说明

Code: 码:

        <ListView Name="playlistListView"  ItemsSource="{Binding PlaylistList}" Margin="0" SelectionChanged="DatabindedPlaylistListView_SelectionChanged" Background="{x:Null}" Opacity="0.98">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <UserControls:SongDataTemplate Margin="4" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

Scenario 3: 方案3:

Here is how it looks like trying to use datatemplates while overriding the theme: 这是在覆盖主题时尝试使用数据模板的样子:

在此处输入图片说明

Code: 码:

<UserControl.Resources>
    <Style x:Key="ListViewItemStretch" TargetType="{x:Type ListViewItem}">
        <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        <Setter Property="Background" Value="Transparent" />
    </Style>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
    <ListView Name="playlistListView" ItemContainerStyle="{StaticResource ListViewItemStretch}" ItemsSource="{Binding PlaylistList}" Margin="0" SelectionChanged="DatabindedPlaylistListView_SelectionChanged" Background="{x:Null}" Opacity="0.98">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <UserControls:SongDataTemplate Margin="4" />
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

I want to keep the theme style but I also want to use datatemplates to define how a playlist should look like. 我想保留主题样式,但我也想使用数据模板来定义播放列表的外观。 Any suggestions? 有什么建议么?

Note: In scenario 2 and 3 I had to remove 注意:在场景2和3中,我必须删除

<ListView.View>
        <GridView>
            <GridViewColumn Width="Auto" DisplayMemberBinding="{Binding Name}">
                <GridViewColumnHeader HorizontalContentAlignment="Left" Content="Playlist" Tag="Playlist"/>
            </GridViewColumn>
        </GridView>
</ListView.View>

Before the datatemplate would be used. 在使用数据模板之前。

Edit: 编辑:

The solution given below, works if the type is changed to ListBox and I am using a TextBox instead. 如果将类型更改为ListBox,而我使用的是TextBox,则下面给出的解决方案有效。 I can't however make it work with a ListView. 但是,我无法使其与ListView一起使用。

You are doing it wrong. 你做错了。 When you want to customize ListView you need to work with the View property which is of type ViewBase. 要自定义ListView时,需要使用ViewBase类型的View属性。 Derive a custom View from ViewBase, assign it to ListView.View and you're done. 从ViewBase派生自定义View,将其分配给ListView.View,您就完成了。 There's an example in ViewBase Class Documentation ViewBase类文档中有一个示例

Try by using BasedOn 通过使用BasedOn尝试

<Style BasedOn={StaticResource {x:Type ListViewItem}} x:Key="ListViewItemStretch" TargetType="{x:Type ListViewItem}"> 
    <Setter Property="HorizontalContentAlignment" Value="Stretch"/> 
    <Setter Property="Background" Value="Transparent" /> 
</Style>

does it work if you substitute 如果您替代它工作吗

<Grid>
   <UserControls:SongDataTemplate Margin="4" />
</Grid>

with a TextBox, for example? 例如,使用TextBox?

The problem could be generated by your user control.. 您的用户控件可能会产生此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM