我有一个DataGrid
我使用了DataGridCell
Style
来为某个列创建工具提示。我需要做的是将不同列的值传递给我使用的ObjectDataProvider
Method
。我想可能有更好的方法,我确信有人会以某种方式建议Converter
。
<UserControl x:Class="TechInventory.ucInv"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:TechInventory"
mc:Ignorable="d"
d:DesignHeight="350" d:DesignWidth="425" Loaded="ucInv_Loaded">
<UserControl.Resources>
<ObjectDataProvider x:Key="PartByLocation" ObjectType="{x:Type local:PartHelper}" MethodName="PartByLocation" IsAsynchronous="True" >
<ObjectDataProvider.MethodParameters >
1234
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
<Style x:Key="cellStyleCenter" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</Style>
<Style x:Key="cellStyleRight" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Right" />
</Style>
<Style x:Key="cellStyleOnHand" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="ToolTipService.ToolTip" >
<Setter.Value>
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" ItemsSource="{Binding Source={StaticResource PartByLocation}}" >
<DataGrid.Columns>
<DataGridTextColumn Header="Location" Binding="{Binding LocationName}" />
<DataGridTextColumn Header="On Hand" Binding="{Binding OnHand}" />
</DataGrid.Columns>
</DataGrid>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="cellStyleHighlight" TargetType="DataGridCell">
<Style.Triggers>
<DataTrigger Binding="{Binding Path=LowQuant}" Value="True">
<Setter Property="Background" Value="PaleVioletRed"/>
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</DataTrigger>
<DataTrigger Binding="{Binding Path=LowQuant}" Value="False" >
<Setter Property="TextBlock.TextAlignment" Value="Center" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid>
<DataGrid Name="dgInv" AutoGenerateColumns="False" IsReadOnly="False" FontSize="15" CanUserResizeRows="False" Margin="10,10,10,10"
AlternatingRowBackground="Cornsilk" SelectionUnit="Cell" MouseLeftButtonUp="dgInv_MouseLeftButtonUp" RowEditEnding="dgInv_RowEditEnding" >
<DataGrid.Columns>
<DataGridTextColumn Header="Part Number" Binding="{Binding PartNumber}" IsReadOnly="True"/>
<DataGridTextColumn Header="Part Description" Binding="{Binding PartDesc}" IsReadOnly="True" />
<DataGridTextColumn Header="On Hand" Binding="{Binding OnHand}" CellStyle="{StaticResource cellStyleOnHand}" />
<DataGridTextColumn Header="QMin" Binding="{Binding QMin}" CellStyle="{StaticResource cellStyleCenter}" />
<DataGridTextColumn Header="QMax" Binding="{Binding QMax}" CellStyle="{StaticResource cellStyleCenter}" />
<DataGridTextColumn Header="To Order" Binding="{Binding ToOrder}" CellStyle="{StaticResource cellStyleHighlight}" IsReadOnly="True" />
<DataGridTextColumn Header="Part SKU" Binding="{Binding PartSKU}" CellStyle="{StaticResource cellStyleRight}" IsReadOnly="True" />
</DataGrid.Columns>
</DataGrid>
</Grid>
PartsHelper使用TableAdapter查询PartNumber(我需要抓取的其他列)。
public class PartHelper
{
private LocationInventoryTableAdapter invLocationAdapter;
private TechinventoryDataSet.LocationInventoryDataTable locPartInvDataTable;
public TechinventoryDataSet.LocationInventoryDataTable PartByLocation(string PartNumber)
{
invLocationAdapter = new LocationInventoryTableAdapter();
locPartInvDataTable = invLocationAdapter.GetDataByPartNumber(PartNumber);
return locPartInvDataTable;
}
}
答案 0 :(得分:0)
我通过使用样式中的EventSetter解决了这一问题。然后,我将样式应用于列。
<Style x:Key="cellStyleOnHand" TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Center" />
<Setter Property="ToolTip" >
<Setter.Value>
<DataGrid AutoGenerateColumns="False" IsReadOnly="True" >
<DataGrid.Columns>
<DataGridTextColumn Header="Location" Binding="{Binding LocationName}" />
<DataGridTextColumn Header="On Hand" Binding="{Binding OnHand}" />
</DataGrid.Columns>
</DataGrid>
</Setter.Value>
</Setter>
<EventSetter Event="ToolTipOpening" Handler="Setter_ToolTipOpening" />
</Style>
<DataGridTextColumn Header="On Hand" Binding="{Binding OnHand}" CellStyle="{StaticResource cellStyleOnHand}" />
后面有代码。计算出该行的零件号,然后使用PartsHelper.PartByLocation填充工具提示网格。
private void Setter_ToolTipOpening(object sender, ToolTipEventArgs e)
{
var dgc = (DataGridCell)sender;
var row = (DataRowView)dgc.DataContext;
string partcell = row["PartNumber"].ToString();
PartHelper partProv = new PartHelper();
(dgc.ToolTip as DataGrid).ItemsSource = partProv.PartByLocation(partcell, site);
}