Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 具有可选样式的Listview_Wpf_Listview_Styles - Fatal编程技术网

Wpf 具有可选样式的Listview

Wpf 具有可选样式的Listview,wpf,listview,styles,Wpf,Listview,Styles,我有一个应用程序,可以选择加载自定义主题。在该主题中,我为ListViewItem提供了一种样式,可以更改高亮显示的颜色。在应用程序中,我有一个GridView,其中的行可以双击,如下所示: <UserControl.Resources> <Style x:Key="ClickableRowStyle" TargetType="{x:Type ListViewItem}"> <EventSetter Event="MouseDoubleCli

我有一个应用程序,可以选择加载自定义主题。在该主题中,我为
ListViewItem
提供了一种样式,可以更改高亮显示的颜色。在应用程序中,我有一个
GridView
,其中的行可以双击,如下所示:

<UserControl.Resources>
    <Style x:Key="ClickableRowStyle" TargetType="{x:Type ListViewItem}">
        <EventSetter Event="MouseDoubleClick" Handler="RowDoubleClicked" />
    </Style>
</UserControl.Resources>

...

<ListView ItemsSource="{Binding DataItems}" ItemContainerStyle="{StaticResource ClickableRowStyle}">

... Set up GridRows 

</ListView>

...
... 设置网格
我遇到的问题是,由于上面的网格使用自己的样式,因此主题中的颜色无法应用

我已尝试添加BasedOn=“{StaticResource{x:Type ListViewItem}}”。如果加载了主题,这是可行的,但是,如果使用默认的Windows样式,我会从StaticResourceHolder获得一个异常


是否有一种方法可以在双击网格行的同时从主题(如果已加载)中获得两种外观?

我找到了如何使用可选样式并同时单击事件的方法。我为列表视图设置了一个加载的事件,在加载的事件中,如果ListViewItem样式已经存在,我将使用BasedOn创建一个新样式

Xaml:


你得到了什么样的例外。如果它是XAMLParseException,请尝试查找它的内部异常以了解确切的问题。在设计器中,我得到{x:Type ListViewItem}无法解析。奇怪。您是否为主题中的样式指定了明确的
x:Key
。如果是,您需要在
BasedOn
中使用该名称。我没有为主题中的样式指定明确的x:Key。只有在未加载主题时才会出现异常,因为WPF似乎没有默认的ListViewItem样式。
<ListView x:Name="listView" ItemsSource="{Binding DataItems}" Loaded="ListView_Loaded">

... Set grid rows

</ListView>
private void ListView_Loaded(object sender RoutedEventArgs e)
{
    ListView listView = sender as ListView;

    Style style;
    if(Application.Current.Resources.Contains(typeof(ListViewItem))
    {
        style = new Style(typeof(ListViewItem), (Style)Application.Current.Resources[typeof(ListViewItem)]);
    }
    else
    {
        style = new Style(typeof(ListViewItem));
    }

    EventSetter setter = new EventSetter();
    setter.Event = ListViewItem.MouseDoubleClickEvent;
    setter.Handler = new MouseButtonEventHandler(ListView_MouseDoubleClick);
    style.Setters.Add(setter);

    listView.ItemContainerStyle = style; 
}