none
Cursor="SizeWE"这个双箭头在GridViewColumnHeader和DataGridColumnHeader表示不一样的问题 RRS feed

  • 问题

  • 各位大侠有碰到过Cursor="SizeWE"这个双箭头在GridViewColumnHeader和DataGridColumnHeader样式中表示不一样的问题吗?能改为一致吗?2个代码如下:

    <!-- DataGridColumnHeader -->

    <Thumb x:Name="PART_RightHeaderGripper"
                                   Cursor="SizeWE"
                                HorizontalAlignment="Right"
                                Background="{DynamicResource {ComponentResourceKey localCodes:HedmSkins, WindowButtonPenColor}}"
                                   Width="1"
                                   Height="15"
                                   VerticalAlignment="Center" >
                                <Thumb.BitmapEffect>
                                    <DropShadowBitmapEffect Color="#000"
                                                            Direction="-90" ShadowDepth="2" Softness="0.1" Opacity="0.3" />
                                </Thumb.BitmapEffect>
                            </Thumb>

     

    <!-- GridViewColumnHeader -->

     <Thumb x:Name="PART_HeaderGripper"
                                   Cursor="SizeWE"
                                HorizontalAlignment="Right"
                                Background="{DynamicResource {ComponentResourceKey localCodes:HedmSkins, WindowButtonPenColor}}"
                                   Width="1"
                                   Height="15"
                                   VerticalAlignment="Center" >
                                <Thumb.BitmapEffect>
                                    <DropShadowBitmapEffect Color="#000"
                                                            Direction="-90" ShadowDepth="2" Softness="0.1" Opacity="0.3" />
                                </Thumb.BitmapEffect>
                            </Thumb>

    2011年7月25日 8:40

答案

  • 找到了,DataGrid用的还是系统定义的,可GridViewColumnHeader用的不是。通过Reflector工具看了下源码:

    GridViewColumnHeader 中有个私有属性,

    private Cursor SplitCursor
    {
      get
      {
        if (_splitCursorCache == null)
        {
          _splitCursorCache = this.GetCursor(100);
        }
        return _splitCursorCache;
      }
    }
    
    


    通过一个静态私有成员来 设置光标:

    private static Cursor _splitCursorCache;
    
    

    这个 this.GetCursor(100) 就返回那个黑色的Resize图标。具体 GetCursor 方法代码如下:

    [SecurityCritical, SecurityTreatAsSafe]
    private Cursor GetCursor(int cursorID)
    {
      Invariant.Assert((cursorID == 100) || (cursorID == 0x65), "incorrect cursor type");
      Cursor cursor = null;
      Stream cursorStream = null;
      Assembly assembly = base.GetType().Assembly;
      if (cursorID == 100)
      {
        cursorStream = assembly.GetManifestResourceStream("split.cur");
      }
      else if (cursorID == 0x65)
      {
        cursorStream = assembly.GetManifestResourceStream("splitopen.cur");
      }
      if (cursorStream != null)
      {
        PermissionSet set = new PermissionSet(null);
        FileIOPermission perm = new FileIOPermission(PermissionState.None) {
          AllLocalFiles = FileIOPermissionAccess.Write
        };
        set.AddPermission(perm);
        set.AddPermission(new EnvironmentPermission(PermissionState.Unrestricted));
        set.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
        set.Assert();
        try
        {
          cursor = new Cursor(cursorStream);
        }
        finally
        {
          CodeAccessPermission.RevertAssert();
        }
      }
      return cursor;
    }
    
     
    
     
    
    

    他读取了一个内嵌资源。

    所以你要使得统一,就只能改GridViewColumnHeader了。所以通过反射把这个类型中的两个静态私有成员的值改掉就好了:

          typeof(GridViewColumnHeader).GetField("_splitOpenCursorCache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).SetValue(nullCursors.SizeWE);
          typeof(GridViewColumnHeader).GetField("_splitCursorCache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).SetValue(nullCursors.SizeWE);

    这个代码放在你的Window的构造函数里面就可以了。

     

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月27日 3:52
    版主

全部回复

  • <Window
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
        mc:Ignorable="d" x:Class="ColorTest.MainWindow"
            Title="MainWindow" Height="350" Width="525">
     <Window.Resources>
            <Style x:Key="123" TargetType="{x:Type GridViewColumnHeader}">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type GridViewColumnHeader}">
                            <Grid>
                                <toolkit:DataGridHeaderBorder BorderBrush="{TemplateBinding BorderBrush}"
                                                          x:Name="dgHeaderBorder">
                                    <ContentPresenter
                                                  Margin="4,0,4,0"/>
                                </toolkit:DataGridHeaderBorder>

                                <!--Style="{DynamicResource ColumnHeaderGripperStyle}"-->
                                <Thumb x:Name="PART_HeaderGripper"
                                   Cursor="SizeWE"
                                HorizontalAlignment="Right"
                                   Width="1"
                                   Height="15"
                                   VerticalAlignment="Center" >
                                    <Thumb.BitmapEffect>
                                        <DropShadowBitmapEffect Color="#000"
                                                            Direction="-90" ShadowDepth="2" Softness="0.1" Opacity="0.3" />
                                    </Thumb.BitmapEffect>
                                </Thumb>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>


        </Window.Resources>
        <Grid>
         <Grid.RowDefinitions>
          <RowDefinition Height="140*"/>
          <RowDefinition Height="4"/>
          <RowDefinition Height="95*"/>
                <RowDefinition Height="14*" />
                <RowDefinition Height="58*" />
            </Grid.RowDefinitions>
                <DataGrid
                                          x:Name="dgPopeDomSet"
                                          AutoGenerateColumns="False"
                                          HeadersVisibility="Column"
                                          SelectionMode="Single"
                                          SelectionUnit="CellOrRowHeader"
                                          CanUserAddRows="False"
                                          CanUserDeleteRows="False"
                                          RowHeaderWidth="0" Margin="0,0,8,9"
                                           d:LayoutOverrides="GridBox"
                                           >
                    <DataGrid.Columns>
                     <DataGridTemplateColumn Header="Test1" Width="50"
                                                        />
                     <DataGridTemplateColumn Header="Test2" Width="100"
                                                         />
                     <DataGridTemplateColumn Header="Test3" 
                      Width="50" />
                     <DataGridTemplateColumn Header="Test4" Width="50"
                                                         />
                     <DataGridTemplateColumn Header="Test5" Width="50"
                                                       />
                </DataGrid.Columns>
                </DataGrid>
               
             <ListView  Name="Func1"
                                           Grid.Row="2" Margin="0,0,0,6">
                                    <ListView.View>
                                        <GridView >
                                            <GridViewColumn >
                                                <GridViewColumnHeader Content="功能列表" Style="{StaticResource 123}"/>
                                                <GridViewColumn.CellTemplate>
                                                    <DataTemplate>
                                                        <Border Height="22">
                                                            <Grid>
                                                                <CheckBox Content="{Binding EntryValue}"
                                                                      IsChecked="{Binding State}"/>
                                                            </Grid>
                                                        </Border>
                                                    </DataTemplate>
                                                </GridViewColumn.CellTemplate>
                                            </GridViewColumn>

                        <GridViewColumn >
                            <GridViewColumnHeader Content="功能列表123" Style="{StaticResource 123}"/>
                            <GridViewColumn.CellTemplate>
                                <DataTemplate>
                                    <Border Height="22">
                                        <Grid>
                                            <CheckBox Content="{Binding EntryValue}"
                                                                      IsChecked="{Binding State}"/>
                                        </Grid>
                                    </Border>
                                </DataTemplate>
                            </GridViewColumn.CellTemplate>
                        </GridViewColumn>
                    </GridView>
                                    </ListView.View>
                                </ListView>

        </Grid>
    </Window>
    GridViewColumnHeader 能改为DataGridColumnHeader 的样式效果

    2011年7月25日 9:29
  • 木有人回答啊!

    我觉得是不是Thumb x:Name="PART_HeaderGripper"和Thumb x:Name="PART_RightHeaderGripper"导致的问题啊?

    2011年7月26日 9:00
  • 我来看看先,别急,马上给你回复。


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月27日 3:21
    版主
  • 找到了,DataGrid用的还是系统定义的,可GridViewColumnHeader用的不是。通过Reflector工具看了下源码:

    GridViewColumnHeader 中有个私有属性,

    private Cursor SplitCursor
    {
      get
      {
        if (_splitCursorCache == null)
        {
          _splitCursorCache = this.GetCursor(100);
        }
        return _splitCursorCache;
      }
    }
    
    


    通过一个静态私有成员来 设置光标:

    private static Cursor _splitCursorCache;
    
    

    这个 this.GetCursor(100) 就返回那个黑色的Resize图标。具体 GetCursor 方法代码如下:

    [SecurityCritical, SecurityTreatAsSafe]
    private Cursor GetCursor(int cursorID)
    {
      Invariant.Assert((cursorID == 100) || (cursorID == 0x65), "incorrect cursor type");
      Cursor cursor = null;
      Stream cursorStream = null;
      Assembly assembly = base.GetType().Assembly;
      if (cursorID == 100)
      {
        cursorStream = assembly.GetManifestResourceStream("split.cur");
      }
      else if (cursorID == 0x65)
      {
        cursorStream = assembly.GetManifestResourceStream("splitopen.cur");
      }
      if (cursorStream != null)
      {
        PermissionSet set = new PermissionSet(null);
        FileIOPermission perm = new FileIOPermission(PermissionState.None) {
          AllLocalFiles = FileIOPermissionAccess.Write
        };
        set.AddPermission(perm);
        set.AddPermission(new EnvironmentPermission(PermissionState.Unrestricted));
        set.AddPermission(new SecurityPermission(SecurityPermissionFlag.UnmanagedCode));
        set.Assert();
        try
        {
          cursor = new Cursor(cursorStream);
        }
        finally
        {
          CodeAccessPermission.RevertAssert();
        }
      }
      return cursor;
    }
    
     
    
     
    
    

    他读取了一个内嵌资源。

    所以你要使得统一,就只能改GridViewColumnHeader了。所以通过反射把这个类型中的两个静态私有成员的值改掉就好了:

          typeof(GridViewColumnHeader).GetField("_splitOpenCursorCache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).SetValue(nullCursors.SizeWE);
          typeof(GridViewColumnHeader).GetField("_splitCursorCache", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).SetValue(nullCursors.SizeWE);

    这个代码放在你的Window的构造函数里面就可以了。

     

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    2011年7月27日 3:52
    版主
  • 非常感谢Bob Bao的回答;问题解决了,果然是高手啊;我弄了半天感觉是wpf本身的问题,以为都无解了。非常感谢
    2011年7月27日 9:16