我编写了自己的用户控件,其中包含一个列表视图,并且其 ItemsSource 是设置运行时。我想从用户控件 xaml 设置我自己的 ListViewItem 样式,但我无法让它工作,无论如何,从 App.xaml 设置它完美无缺,所以我想知道为什么?
用户控件 xaml 如下所示:
<UserControl x:Class="LogManagerWPF.LogManager"
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:LogManagerWPF"
mc:Ignorable="d"
d:DesignHeight="192" d:DesignWidth="400"
DataContext="{Binding RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<DataTemplate x:Key="DisplayImage">
<Image Source="{Binding ImageIcon}" Width="16" Height="16"/>
</DataTemplate>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</UserControl.Resources>
<ListView Margin="0" x:Name="m_lvLog" ScrollViewer.VerticalScrollBarVisibility="Auto" ScrollViewer.HorizontalScrollBarVisibility="Auto" ContextMenuOpening="m_lvLog_ContextMenuOpening" SelectionChanged="m_lvLog_SelectionChanged">
<ListView.Resources>
<!-- Windows 7 -->
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />
<SolidColorBrush x:Key="{x:Static SystemColors.ActiveBorderBrushKey}" Color="{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}" />
<!-- Windows 10 -->
<Style TargetType="{x:Type ListViewItem}">
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background">
<Setter.Value>
<SolidColorBrush Color="{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"></SolidColorBrush>
</Setter.Value>
</Setter>
<Setter Property="BorderBrush">
<Setter.Value>
<SolidColorBrush Color="{Binding SelectedRowBackColor, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"></SolidColorBrush>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</ListView.Resources>
<ListView.View>
<GridView x:Name="m_gvLog" AllowsColumnReorder="False">
<GridViewColumn x:Name="m_icon" Width="28" Header="" CellTemplate="{StaticResource DisplayImage}"/>
<GridViewColumn x:Name="m_time" Width="Auto" Header="Timestamp" DisplayMemberBinding="{Binding Timestamp}">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Foreground" Value="#FF042271" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Padding" Value="10,0,0,0" />
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
<GridViewColumn x:Name="m_messages" Width="100" Header="Message" DisplayMemberBinding="{Binding Message}">
<GridViewColumn.HeaderContainerStyle>
<Style TargetType="{x:Type GridViewColumnHeader}">
<Setter Property="Foreground" Value="#FF042271" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="Padding" Value="10,0,0,0" />
</Style>
</GridViewColumn.HeaderContainerStyle>
</GridViewColumn>
</GridView>
</ListView.View>
<ListView.ContextMenu>
<ContextMenu x:Name="m_contextMenu">
<MenuItem x:Name="m_menuAutoScroll" Header="Autoscroll" Click="m_menuAutoScroll_Click" IsCheckable="True" />
<MenuItem x:Name="m_menuClear" Header="Clear Log" Click="m_menuClear_Click"/>
<MenuItem x:Name="m_menuCopy" Header="Copy to Clipboard" Click="m_menuCopy_Click"/>
<MenuItem x:Name="m_menuOpenLog" Header="Open Log" Click="m_menuOpenLog_Click"/>
<MenuItem x:Name="m_menuHorizontalScrollBarVisibility" Header="Set Horizontal ScrollBar Visibility">
<MenuItem x:Name="m_menuHorizontalScrollBarEnabled" Header="Enabled" Click="m_menuHorizontalScrollBarEnabled_Click" IsCheckable="True"/>
<MenuItem x:Name="m_menuHorizontalScrollBarDisabled" Header="Disabled" Click="m_menuHorizontalScrollBarDisabled_Click" IsCheckable="True"/>
</MenuItem>
<MenuItem x:Name="m_menuVerbosity" Header="Set Verbosity">
<MenuItem x:Name="m_menuVerbosityLow" Header="Low" Click="m_menuVerbosityLow_Click" IsCheckable="True"/>
<MenuItem x:Name="m_menuVerbosityStandard" Header="Standard" Click="m_menuVerbosityStandard_Click" IsCheckable="True"/>
<MenuItem x:Name="m_menuVerbosityHigh" Header="High" Click="m_menuVerbosityHigh_Click" IsCheckable="True"/>
<MenuItem x:Name="m_menuVerbosityDebug" Header="Debug" Click="m_menuVerbosityDebug_Click" IsCheckable="True"/>
</MenuItem>
<MenuItem x:Name="m_menuSeverityFilter" Header="Set Severity Filter">
<MenuItem x:Name="m_menuSeverityFilterDebug" Header="Debug" Checked="m_menuSeverityFilterDebug_CheckedChanged" Unchecked="m_menuSeverityFilterDebug_CheckedChanged" IsCheckable="True" StaysOpenOnClick="True"/>
<MenuItem x:Name="m_menuSeverityFilterInformation" Header="Information" Checked="m_menuSeverityFilterInformation_CheckedChanged" Unchecked="m_menuSeverityFilterInformation_CheckedChanged" IsCheckable="True" StaysOpenOnClick="True"/>
<MenuItem x:Name="m_menuSeverityFilterWarning" Header="Warning" Checked="m_menuSeverityFilterWarning_CheckedChanged" Unchecked="m_menuSeverityFilterWarning_CheckedChanged" IsCheckable="True" StaysOpenOnClick="True"/>
<MenuItem x:Name="m_menuSeverityFilterError" Header="Error" Checked="m_menuSeverityFilterError_CheckedChanged" Unchecked="m_menuSeverityFilterError_CheckedChanged" IsCheckable="True" StaysOpenOnClick="True"/>
<Separator/>
<MenuItem x:Name="m_menuSeverityFilterAll" Header="All" Click="m_menuSeverityFilterAll_Click" StaysOpenOnClick="True"/>
<MenuItem x:Name="m_menuSeverityFilterNone" Header="None" Click="m_menuSeverityFilterNone_Click" StaysOpenOnClick="True"/>
</MenuItem>
<MenuItem x:Name="m_menuContentFilter" Header="Set Content Filter">
<MenuItem x:Name="m_menuContentFilterSet">
<MenuItem.Header>
<StackPanel Orientation="Horizontal">
<Label Content="Set" Padding="0"/>
<TextBox x:Name="m_menuContentFilterString" Width="150" Padding="0" Margin="10,0,0,0" VerticalContentAlignment="Center" LostFocus="m_menuContentFilterString_LostFocus"/>
</StackPanel>
</MenuItem.Header>
</MenuItem>
<MenuItem x:Name="m_menuContentFilterClear" Header="Clear" Click="m_menuContentFilterClear_Click"/>
</MenuItem>
</ContextMenu>
</ListView.ContextMenu>
</ListView>
</UserControl>
我想应用的样式是这样的:
<Application x:Class="LogManagerWpf_Tester.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Center" />
</Style>
</Application.Resources>
</Application>
我知道当从用户控件本身执行样式时,不会应用该样式,因为在输出窗口中,我看到添加的每个ListViewItem都出现以下错误,而当从App.xaml应用样式时,这些错误不会显示:
System.Windows.Data 错误: 4 : 找不到引用“RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1'”进行绑定的源。绑定表达式:路径=水平内容对齐;数据项=空;目标元素是“列表视图项”(名称=“);目标属性为“水平内容对齐”(类型为“水平对齐”)
系统。Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,ancestor type =”System的绑定的源。Windows.Controls.ItemsControl ',AncestorLevel='1 ' '。binding expression:Path = VerticalContentAlignment;DataItem = null目标元素是“ListViewItem”(名称=“”);目标属性为“VerticalContentAlignment”(类型为“VerticalAlignment”)
我不喜欢编辑一个应用程序的App.xaml文件来放置一个修复用户控件问题的样式,因为那样我将不得不编辑使用这种控件的每个应用程序。幸运的是,我找到了一个放置在用户控件构造器中的变通方法,它允许我在不编辑应用程序xaml文件的情况下解决这个问题:
Style listViewItemStyle = new Style(typeof(ListViewItem));
listViewItemStyle.Setters.Add(new Setter(ListViewItem.HorizontalContentAlignmentProperty, HorizontalAlignment.Stretch));
listViewItemStyle.Setters.Add(new Setter(ListViewItem.VerticalContentAlignmentProperty, VerticalAlignment.Center));
if (!Application.Current.Resources.Contains(typeof(ListViewItem)))
{
Application.Current.Resources.Add(typeof(ListViewItem), listViewItemStyle);
}
虽然上面的代码有效,但我想知道为什么我不能直接从用户控件 xaml 获得所需的结果。
问题内容: 嗨,我只是简单地尝试在www.example.com上获取h1标签,该标签显示为“ Example Domain”。该代码适用于http://www.example.com,但不适用于https://www.exmaple.com。我该如何解决这个问题?谢谢 问题答案: PhantomJSDriver不支持(所有)DesiredCapabilities。 你会需要: 记录在这里:htt
所以我使用这种方法写入文件,它在windows上运行完全正常,但在mac上运行时,它会创建文件,但它们是空的。 我知道数据是正确的,因为它打印正确。感谢您的任何帮助,这真的让我绊倒了。
列名称的类型为int[] 上述查询适用于postgresql,但不适用于hsqldb,甚至适用于sql 尝试的hsqldb版本:2.2.9和2.3.0 在hsqldb中工作的sql是从table_name中选择x,unnest(column_name)y(x)x和y不是该表的列。
我能够成功地打电话给邮递员: /mfp/api/az/v1/token和 /mfpadmin/management-apis/2.0/runtimes/mfp/applications 我正在获取从/mfp/api/az/v1/token接收的承载令牌,并将其添加到/mfp/applications的授权标头中。 我收到了来自两者的200个响应,并从每个API中获取了预期的信息。 然后,我选择从P
我一直在使用声纳3.2 同样的配置,当我升级到SonarQube 4.4时 声纳项目属性: 请帮助整理这些例外 问候, KP
我已经看过并尝试了几乎所有关于这个话题的其他帖子,但运气不好。 我使用的是python 3.6,所以我使用的是以下AMI