WPF常用技巧汇总 - Part 2

WPF常用技巧汇总-CSDN博客

主要用于记录工作中发现的一些问题和常见的解决方法。

目录

WPF常用技巧汇总-CSDN博客

[1. DataGrid Tooltip - Multiple](#1. DataGrid Tooltip - Multiple)

[2. DataGrid Tooltip - Cell值和ToolTip值一样](#2. DataGrid Tooltip - Cell值和ToolTip值一样)

[3. DataGrid Tooltip - Cell值和ToolTip值不一样](#3. DataGrid Tooltip - Cell值和ToolTip值不一样)

[4. DataGrid - Ctrl +A /Ctrl + C复制的内容不是Cell的值](#4. DataGrid - Ctrl +A /Ctrl + C复制的内容不是Cell的值)

[5. DataGrid - Edit -Combox](#5. DataGrid - Edit -Combox)

[6. DataGrid - 解决DataGridTemplateColumn无法排序的问题](#6. DataGrid - 解决DataGridTemplateColumn无法排序的问题)


1. DataGrid Tooltip - Multiple

XML 复制代码
 <DataGridTemplateColumn MinWidth="120" x:Name="colExclude">
     <DataGridTemplateColumn.HeaderTemplate>
         <DataTemplate>
             <Grid>
                 <Grid.RowDefinitions>
                     <RowDefinition Height="*"/>
                     <RowDefinition Height="*"/>
                 </Grid.RowDefinitions>
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                     <ColumnDefinition Width="*"></ColumnDefinition>
                 </Grid.ColumnDefinitions>
                 <Label Style="{StaticResource LabelHeaderWhiteColorStyle}" Grid.Row="0" Grid.ColumnSpan="2" HorizontalAlignment="Center" Content="Exclude"/>
                 <Border Grid.Row="0" Grid.Column="2" Width="22" Margin="0,2" ToolTipService.ShowDuration="50000">
                     <Border.Background>
                         <ImageBrush ImageSource="..\..\..\Images\question_mark.png"/>
                     </Border.Background>
                     <Border.ToolTip>
                         <ToolTip>
                             <StackPanel>
                                 <Label Content="Tool Tip:" HorizontalAlignment="Center"/>
                                 <Label Content="Notice = Exclude Notice: Lender selected will not be available in the Notice Preview"/>
                                 <Label Content="JobTicket = Exclude Job Ticket: Lender selected will not be available in the Job Ticket Preview"/>
                                 <Label Content="Recon = Exclude Distribution: Lender selected will decrease the Total Cash Actual expected in Reconciliation"/>
                             </StackPanel>
                         </ToolTip>
                     </Border.ToolTip>
                 </Border>
                 <Label Style="{StaticResource LabelHeaderWhiteColorStyle}" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" Content="Notice"/>
                 <Label Style="{StaticResource LabelHeaderWhiteColorStyle}" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Content="JobTicket"/>
                 <Label Style="{StaticResource LabelHeaderWhiteColorStyle}" Grid.Row="1" Grid.Column="2" HorizontalAlignment="Left" Content="Recon"/>
             </Grid>
         </DataTemplate>
     </DataGridTemplateColumn.HeaderTemplate>
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <Grid IsEnabled="{Binding DataContext.IsCashDistributeEnable, RelativeSource={RelativeSource AncestorType={x:Type bs:AgencyWindowBase}},Mode=OneWay}">
                 <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="*"/>
                     <ColumnDefinition Width="*"/>
                     <ColumnDefinition Width="*"/>
                 </Grid.ColumnDefinitions>
                 <CheckBox Style="{StaticResource CheckBoxInCellStyle}" HorizontalAlignment="Left" Grid.Column="0" IsChecked="{Binding DataContext.ExcludeFromNotice, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Mode=TwoWay}" IsThreeState="False" IsEnabled="{Binding DataContext.IsExcludeNoticeEnabled, RelativeSource={RelativeSource AncestorType={x:Type bs:AgencyWindowBase}},Mode=OneWay}"/>
                 <CheckBox Style="{StaticResource CheckBoxInCellStyle}" Grid.Column="1" IsChecked="{Binding DataContext.ExcludeFromJobTicket, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Mode=TwoWay}" IsThreeState="False" IsEnabled="{Binding DataContext.IsExcludeJTAndReconEnable, RelativeSource={RelativeSource AncestorType={x:Type bs:AgencyWindowBase}},Mode=OneWay}" HorizontalAlignment="Center"/>
                 <CheckBox Style="{StaticResource CheckBoxInCellStyle}" HorizontalAlignment="Right" Grid.Column="2" IsChecked="{Binding DataContext.ExcludeFromRecon, RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}},Mode=TwoWay}" IsThreeState="False" IsEnabled="{Binding DataContext.IsExcludeJTAndReconEnable, RelativeSource={RelativeSource AncestorType={x:Type bs:AgencyWindowBase}},Mode=OneWay}"/>
             </Grid>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
 </DataGridTemplateColumn>

2. DataGrid Tooltip - Cell值和ToolTip值一样

XML 复制代码
<DataGridTextColumn Header="Borrower Name" Binding="{Binding BorrowerName}" IsReadOnly="True" MinWidth="150" Width="150" MaxWidth="1000">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding BorrowerName}"></Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

3. DataGrid Tooltip - Cell值和ToolTip值不一样

XML 复制代码
<DataGridTextColumn Header="Borrower Name" Binding="{Binding BorrowerName}" IsReadOnly="True" MinWidth="150" Width="150" MaxWidth="1000">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="ToolTip" Value="{Binding BorrowerNameToolTip}"></Setter>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

4. DataGrid - Ctrl +A /Ctrl + C复制的内容不是Cell的值

默认copy的内容是Cell的value, 即Binding的field的value。

但是有时候我们想copy的是其他的field值,怎么办?

比如我们这个列显示的是一些内容的缩写,但是拷贝的时候需要拷贝全整的内容。

此时可以通过指定ClipboardContentBinding去解决此类问题。

XML 复制代码
 <DataGridTextColumn Header="Sending To" Binding="{Binding singleRecipient}" IsReadOnly="True" MinWidth="200" Width="240" ClipboardContentBinding="{Binding Recipients}">
     <DataGridTextColumn.CellStyle>
         <Style TargetType="DataGridCell">
             <Setter Property="ToolTip" Value="{Binding Recipients}"></Setter>
         </Style>
     </DataGridTextColumn.CellStyle>
 </DataGridTextColumn>

5. DataGrid - Edit -Combox

XML 复制代码
 <DataGridTemplateColumn Width="180" IsReadOnly="False">
     <DataGridTemplateColumn.HeaderTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal">
                 <TextBlock FontWeight="SemiBold" Text="Account Name "/>
                 <Image Source="..\..\..\Images\question_mark.png" Height="15" Width="15" HorizontalAlignment="Right" Grid.Column="1" 
    ToolTipService.ToolTip="Bla bla bla..." />
             </StackPanel>
         </DataTemplate>
     </DataGridTemplateColumn.HeaderTemplate>
     <DataGridTemplateColumn.CellTemplate>
         <DataTemplate>
             <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                 <Label ToolTip="{Binding  SelectedAccount.Name}" Content="{Binding SelectedAccount.Name}" 
                Visibility="{Binding Path=IsAccountEdit, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=false}"></Label>
             </StackPanel>
         </DataTemplate>
     </DataGridTemplateColumn.CellTemplate>
     <DataGridTemplateColumn.CellEditingTemplate>
         <DataTemplate>
             <ComboBox Visibility="{Binding Path=IsAccountEditShow, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=false}" Width="170" SelectedValue="{Binding SelectedAccount, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}" x:Name="cbxSelectedAcount" SelectionChanged="cbxSelectedAcount_SelectionChanged"
   ItemsSource="{Binding Path=Accounts, UpdateSourceTrigger=PropertyChanged}"
   IsEnabled="{Binding DataContext.NoticeTemplate.Payment.IsTypeBorrowingOIDAndHasAgentAccountId, Converter={StaticResource InverseBooleanConverter}, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}">
                 <ComboBox.ItemTemplate>
                     <DataTemplate>
                         <TextBlock Text="{Binding Account.Name}" Foreground="Black"/>
                     </DataTemplate>
                 </ComboBox.ItemTemplate>
             </ComboBox>
         </DataTemplate>
     </DataGridTemplateColumn.CellEditingTemplate>
 </DataGridTemplateColumn>
XML 复制代码
      private void cbxSelectedAcount_SelectionChanged(object sender, SelectionChangedEventArgs e)
      {
          if (e.AddedItems.Count > 0)
          {
              var selectedAccount = e.AddedItems[0] as AccountDto;
          }
      }

6. DataGrid - 解决DataGridTemplateColumn无法排序的问题

DataGrid设置了CanUserSortColumns="True"后,DataGridTemplateColumn点击Header后无法支持排序。此时可通过指定CanUserSort="True" 和SortMemberPath="SelectedAccount.Name"解决。

XML 复制代码
      <DataGridTemplateColumn Width="180" IsReadOnly="False" CanUserSort="True" SortMemberPath="SelectedAccount.Name" >
          <DataGridTemplateColumn.HeaderTemplate>
              <DataTemplate>
                  <StackPanel Orientation="Horizontal">
                      <TextBlock FontWeight="SemiBold" Text="Account Name "/>
                      <Image Source="..\..\..\Images\question_mark.png" Height="15" Width="15" HorizontalAlignment="Right" Grid.Column="1" 
         ToolTipService.ToolTip="Wire Instructions of the selected Account will be used on the Notice.&#x0a;&#x0a;Accounts are setup at the Facility Level under the Lender's Admin Details.&#x0a;&#x0a;If an Account is unavailable:&#x0a;1) Check if its Currency matches the Payment Currency&#x0a;2) Check it's selected for ALL applicable Payment Types" />
                  </StackPanel>
              </DataTemplate>
          </DataGridTemplateColumn.HeaderTemplate>
          <DataGridTemplateColumn.CellTemplate>
              <DataTemplate>
                  <StackPanel Orientation="Horizontal" HorizontalAlignment="Left">
                      <Label ToolTip="{Binding  SelectedAccount.Name}" Content="{Binding SelectedAccount.Name}" 
                     Visibility="{Binding Path=IsAccountEdit, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=false}"></Label>
                  </StackPanel>
              </DataTemplate>
          </DataGridTemplateColumn.CellTemplate>
          <DataGridTemplateColumn.CellEditingTemplate>
              <DataTemplate>
                  <ComboBox Visibility="{Binding Path=IsAccountEditShow, Converter={StaticResource BooleanToVisibilityConverter}, ConverterParameter=false}"  Width="170" SelectedValue="{Binding SelectedAccount, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}, Mode=FindAncestor}}" x:Name="cbxSelectedAcountForLender" SelectionChanged="cbxSelectedAcountForLender_SelectionChanged"
        ItemsSource="{Binding Path=Accounts, UpdateSourceTrigger=PropertyChanged}"
        >
                      <ComboBox.ItemTemplate>
                          <DataTemplate>
                              <TextBlock Text="{Binding Account.Name}" Width="150" Foreground="Black"/>
                          </DataTemplate>
                      </ComboBox.ItemTemplate>
                  </ComboBox>
              </DataTemplate>
          </DataGridTemplateColumn.CellEditingTemplate>
      </DataGridTemplateColumn>
相关推荐
小雅痞10 分钟前
[Java][Leetcode middle] 134. 加油站
java·leetcode
Xiaoshuang_Cao13 分钟前
maven之pom.xml
xml·java·maven
重生之后端学习20 分钟前
03-Web后端基础(Maven基础)
java·前端·spring boot·后端·spring·tomcat·maven
gaosushexiangji20 分钟前
实验分享|基于千眼狼sCMOS科学相机的流式细胞仪细胞核成像实验
大数据·人工智能·科技·数码相机·计算机视觉
JosieBook22 分钟前
【web应用】配置Java JDK与maven3的环境变量
java·开发语言
全栈凯哥28 分钟前
Java详解LeetCode 热题 100(18):LeetCode 73. 矩阵置零(Set Matrix Zeroes)详解
java·算法·leetcode
往日时光--35 分钟前
Centos上搭建 OpenResty
java
普通的冒险者38 分钟前
用java实现内网通讯,可多开客户端链接同一个服务器
java·开发语言
Muroidea38 分钟前
解决RedisTemplate的json反序列泛型丢失问题
java·开发语言·json
雪碧聊技术1 小时前
在SpringBoot项目中,使用单元测试@Test
java·spring boot·单元测试