ListViewItem的ComponentResourceKey

时间:2019-01-07 10:33:11

标签: c# wpf

首先,我试图获取ListView的行详细信息(就像RowDetailsTemplate一样)功能,并且正在使用这种方法:

gridview-equivalent-of-the-datagrids-row-details-template

自定义功能在此处定义:

public class GridViewWithRowDetails : GridView
{
    public static DataTemplate GetRowDetailsTemplate(DependencyObject obj)
    {
        return (DataTemplate)obj.GetValue(RowDetailsTemplateProperty);
    }

    public static void SetRowDetailsTemplate(DependencyObject obj, DataTemplate value)
    {
        obj.SetValue(RowDetailsTemplateProperty, value);
    }

    public static readonly DependencyProperty RowDetailsTemplateProperty =
        DependencyProperty.RegisterAttached("RowDetailsTemplate", typeof(DataTemplate), typeof(GridViewWithRowDetails), new UIPropertyMetadata());


    protected override void PrepareItem(ListViewItem item)
    {
        base.PrepareItem(item);
        item.SetValue(RowDetailsTemplateProperty, this.GetValue(RowDetailsTemplateProperty));

    }

    protected override object ItemContainerDefaultStyleKey
    {
        get
        {
            return new ComponentResourceKey(this.GetType(), "ItemContainerStyleKey");
        }
    }
}

和主要样式:

<Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type themes:GridViewWithRowDetails}, 
       ResourceId=ItemContainerStyleKey}"
       TargetType="{x:Type ListViewItem}">

存在的内容定义如下:

<ContentPresenter ContentTemplate="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListViewItem}, Path=(themes:GridViewWithRowDetails.RowDetailsTemplate), Mode=TwoWay}" 
                                              Visibility="Visible"
                                              Grid.Row="2" 
                                              Content="{TemplateBinding Content}"/>

我将“可见性”设置为“可见”,以确保不会发生绑定问题。

我正在这样使用它:

<themes:GridViewWithRowDetails>
    <themes:GridViewWithRowDetails.RowDetailsTemplate>
         <DataTemplate>
               <StackPanel Background="Black">
                      <TextBox Margin="10, 2" Text="text"/>
               </StackPanel>
          </DataTemplate>
    </themes:GridViewWithRowDetails.RowDetailsTemplate>

     <themes:GridViewWithRowDetails.Columns>
     ..........................
     </themes:GridViewWithRowDetails.Columns>

</themes:GridViewWithRowDetails>

我的项目设置为使用wpf自定义控件,并且其中包含程序集:ThemeInfo节。

我不能使用

DefaultStyleKeyProperty.OverrideMetadata(typeof(MyCustomControl), 
        new FrameworkPropertyMetadata(typeof(MyCustomControl)));

这似乎为其他人解决了此问题,因为GridViewWithRowDetails类是从GridView派生的,因此它缺少DefaultStyleKeyProperty。

我认为DefaultStyleKeyProperty是为基于Control的类定义的。

我不得不提一下,如果我在MainApp中执行所有这些操作,那么效果很好。

任何提示将不胜感激。

1 个答案:

答案 0 :(得分:0)

好,所以MahApps正在发生碰撞。在我的主要应用程序App.xaml中,

<ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.xaml" />

我删除了它,取而代之的是我定义了一个具有以下内容的新字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ListView.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Scrollbars.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Buttons.xaml" />

    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.CheckBox.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ComboBox.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ContextMenu.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.PasswordBox.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.ProgressBar.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.RadioButton.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Slider.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TabControl.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TextBlock.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.TextBox.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MahApps.Metro;component/Styles/Controls.Tooltip.xaml" />
</ResourceDictionary.MergedDictionaries>

<ControlTemplate x:Key="ValidationToolTipTemplate">
    <Grid x:Name="Root" Margin="5,0" Opacity="0" RenderTransformOrigin="0,0">
        <Grid.RenderTransform>
            <TranslateTransform x:Name="xform" X="-25" />
        </Grid.RenderTransform>
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="OpenStates">
                <VisualStateGroup.Transitions>
                    <VisualTransition GeneratedDuration="0" />
                    <VisualTransition GeneratedDuration="0:0:0.2" To="Open">
                        <Storyboard>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="xform">
                                <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="0" />
                            </DoubleAnimationUsingKeyFrames>
                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root">
                                <SplineDoubleKeyFrame KeyTime="0:0:0.2" Value="1" />
                            </DoubleAnimationUsingKeyFrames>
                        </Storyboard>
                    </VisualTransition>
                </VisualStateGroup.Transitions>
                <VisualState x:Name="Closed">
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root">
                            <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
                <VisualState x:Name="Open">
                    <Storyboard>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="X" Storyboard.TargetName="xform">
                            <SplineDoubleKeyFrame KeyTime="0" Value="0" />
                        </DoubleAnimationUsingKeyFrames>
                        <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity" Storyboard.TargetName="Root">
                            <SplineDoubleKeyFrame KeyTime="0" Value="1" />
                        </DoubleAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Border Background="{DynamicResource ValidationBrush1}" CornerRadius="5" Margin="4,4,-4,-4" />
        <Border Background="{DynamicResource ValidationBrush2}" CornerRadius="4" Margin="3,3,-3,-3" />
        <Border Background="{DynamicResource ValidationBrush3}" CornerRadius="3" Margin="2,2,-2,-2" />
        <Border Background="{DynamicResource ValidationBrush4}" CornerRadius="2" Margin="1,1,-1,-1" />
        <Border Background="{DynamicResource ValidationBrush5}" CornerRadius="2" /> 
        <Border CornerRadius="2">
            <TextBlock Foreground="{DynamicResource WhiteColorBrush}"
                       MaxWidth="250"
                       Margin="8,4,8,4"
                       TextWrapping="Wrap"
                       Text="{Binding (Validation.Errors)[0].ErrorContent}"
                       UseLayoutRounding="false" />
        </Border>
    </Grid>
</ControlTemplate>

   <Style TargetType="ToolTip" BasedOn="{StaticResource MetroToolTip}" />
   <Style TargetType="Button" BasedOn="{StaticResource MetroButton}" />
   <Style TargetType="ToggleButton" BasedOn="{StaticResource MetroToggleButton}" />
   <Style TargetType="TextBox" BasedOn="{StaticResource MetroTextBox}" />
   <Style TargetType="Slider" BasedOn="{StaticResource MetroSlider}" />
   <Style TargetType="TextBlock" BasedOn="{StaticResource MetroTextBlock}" />
   <Style TargetType="TabItem" BasedOn="{StaticResource MetroTabItem}"  />
   <Style TargetType="TabControl" BasedOn="{StaticResource MetroTabControl}" />
   <!--<Style TargetType="ScrollBar" BasedOn="{StaticResource MetroScrollBar}" />-->
   <Style TargetType="RadioButton" BasedOn="{StaticResource MetroRadioButton}" />
   <Style TargetType="ProgressBar" BasedOn="{StaticResource MetroProgressBar}" />
   <Style TargetType="PasswordBox" BasedOn="{StaticResource MetroPasswordBox}" />
   <Style TargetType="CheckBox" BasedOn="{StaticResource MetroCheckBox}" />
   <Style TargetType="ComboBox" BasedOn="{StaticResource MetroComboBox}" />
   <Style TargetType="ComboBoxItem" BasedOn="{StaticResource MetroComboBoxItem}" />
   <Style TargetType="MenuItem" BasedOn="{StaticResource MetroMenuItem}" />
   <Style TargetType="ContextMenu" BasedOn="{StaticResource MetroContextMenu}" />
</ResourceDictionary>

无论如何,这是一件很奇怪的事情,因为ListView.xaml仍然存在,但是我将继续对此进行调查。目前,我有一个工作版本。