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.

Accounts are setup at the Facility Level under the Lender's Admin Details.

If an Account is unavailable:
1) Check if its Currency matches the Payment Currency
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>