我对WPF很新,所以如果这个问题的答案很简单,我会提前道歉。我到处寻找,但无法找到合适的解决方案。
低调: 我有一个使用MahApps的应用程序。我的应用程序中的Windows工作正常。我可以设置样式和更改主题,它适用于所有窗口。 问题是在我的MainWindow中我有几个UserControls。其中一个UserControl有一个DataGrid,由于某种原因,DataGrid不会采用默认的MahApps DataGrid样式。
另一件事:我的DataGrid使用DataTriggers显示不同的可观察列表,具体取决于已按下的RadioButton。因此,它不允许我设置DataGrid的样式(根据MahApps指南:http://mahapps.com/controls/datagrid.html)。
MahApps的建议:
<DataGrid ItemsSource="{Binding People}" Margin="10,20,10,0"
AutoGenerateColumns="True"
Style="{StaticResource AzureDataGrid}">
我发现的另一个建议是将MahApps资源添加到UserControl的资源字典中。这没用,所以我删除了那段代码。
我目前的UserControl xaml:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TrayportLibrary.Home"
xmlns:helpers="clr-namespace:TrayportLibrary.Helpers"
xmlns:controls="clr-namespace:MahApps.Metro.Controls;assembly=MahApps.Metro"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:ei="http://schemas.microsoft.com/expression/2010/interactions"
x:Class="TrayportLibrary.Search.SearchView"
mc:Ignorable="d" >
<UserControl.Resources>
<helpers:StringToVisibilityConverter x:Key="StringToVisibilityConverter"/>
<BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
</UserControl.Resources>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="LoadCollections"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel FocusManager.FocusedElement="{Binding ElementName=SearchBox}">
<Grid Name="MainGrid">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<TextBox x:Name="SearchBox"
Focusable="True"
VerticalAlignment="Center"
HorizontalAlignment="Right"
Text="{Binding SearchInput, UpdateSourceTrigger=PropertyChanged}"
Width="200"
Height="25"
BorderBrush="Black"
BorderThickness="2"
Grid.Column="0"
Grid.Row="0"
Margin="0,0,5,0"/>
<StackPanel Grid.Column="1"
Grid.Row="0"
Orientation="Horizontal"
VerticalAlignment="Center"
HorizontalAlignment="Left">
<RadioButton GroupName="SearchSelection"
Name="BookRadio"
Content="Books"
Margin="0,0,5,0"
IsChecked="{Binding SearchBook}"
Command="{Binding SetSearchCommand}"
CommandParameter="book"
Click="RadioButton_OnClick"/>
<RadioButton GroupName="SearchSelection"
Name="MemberRadio"
Content="Members"
IsChecked="{Binding SearchMember}"
Command="{Binding SetSearchCommand}"
CommandParameter="member"
Click="RadioButton_OnClick"/>
</StackPanel>
<FrameworkElement Grid.Row="1" Grid.Column="0" x:Name="Proxy"/>
<DataGrid Name="DataGrid"
Grid.Row="1"
Grid.ColumnSpan="2"
Grid.Column="0"
IsReadOnly="True"
AutoGenerateColumns="False"
Height="200"
VerticalScrollBarVisibility="Auto"
Visibility="{Binding Path=Text,
ElementName=SearchBox,
Converter={StaticResource
StringToVisibilityConverter}}">
<DataGrid.Columns>
<DataGridTextColumn Header="Book ID" Binding="{Binding BookID}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Title" Binding="{Binding Title}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Author(s)" Binding="{Binding AuthorData}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Date Due" Binding="{Binding DateDue, StringFormat=dd/MM/yyyy}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridCheckBoxColumn Header="Out" Binding="{Binding IsOut}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="On Loan to" Binding="{Binding OnLoanToUserName}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridCheckBoxColumn Header="Lost" Binding="{Binding IsLost}" Visibility="{Binding DataContext.BookVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Visibility="{Binding DataContext.MemberVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Username" Binding="{Binding UserName}" Visibility="{Binding DataContext.MemberVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Books on Loan" Binding="{Binding BooksOnLoan}" Visibility="{Binding DataContext.MemberVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
<DataGridTextColumn Header="Books Overdue" Binding="{Binding BooksOverdue}" Visibility="{Binding DataContext.MemberVisibility, Converter={StaticResource BooleanToVisibilityConverter}, Source={x:Reference Proxy}, Mode=TwoWay, NotifyOnSourceUpdated=True}"/>
</DataGrid.Columns>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<ei:CallMethodAction MethodName="OnSelectedItem" TargetObject="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=MemberRadio, Path=IsChecked}" Value="true">
<Setter Property="DataGrid.ItemsSource" Value="{Binding Members}"/>
<Setter Property="DataGrid.SelectedItem" Value="{Binding SelectedMember}"/>
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=BookRadio, Path=IsChecked}" Value="true">
<Setter Property="DataGrid.ItemsSource" Value="{Binding Books}"/>
<Setter Property="DataGrid.SelectedItem" Value="{Binding SelectedBook}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.Style>
</DataGrid>
</Grid>
</StackPanel>
任何建议/代码/最佳实践将不胜感激。 我也意识到我当前的xaml可能会变得更简单,但我只是想在开始重构之前先解决这个问题。
提前致谢。
答案 0 :(得分:1)
问题是您要覆盖通常使用自己的自定义样式应用于DataGrid的样式。
您的自定义样式可以使用BasedOn属性继承其他样式的setter。
例如:
<Style BasedOn="{StaticResource AzureDataGrid}">
或者继承控件类型的默认样式:
<Style BasedOn="{StaticResource {x:Type DataGrid}}">