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>
相关推荐
qq_452396233 小时前
第三篇:Transactional Outbox 模式详解
java·kubernetes·rocketmq
崖边看雾3 小时前
Git 手册:三区、分支、撤销、冲突与团队协作
大数据·git·elasticsearch
3095566664 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
凌镜icecraft5 小时前
我改文件扩展名的时候那个警告弹窗再也不会弹出来了
windows·c#·wpf·explorer
摇滚侠10 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 21
java·开发语言·笔记
凌冰_12 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
Rain的Java大神之路13 小时前
Spring AI 2.0 GA 深度复盘:Java 后端做 AI 的分水岭,到底画在哪?
java·后端·aigc
starzy199014 小时前
Structured Streaming Kafka Sink:写出一条生产级的回流链路
大数据·分布式·spark·kafka
数据世界114 小时前
从立项选品到竞争情报:医药人查询国内品种格局数据的难点与5.0版结构化整理
大数据·数据库·人工智能