【发布时间】:2019-05-14 02:42:30
【问题描述】:
我的应用程序的最终目标是让用户比较 2 个数据表。我有 2 个 DataGrids 并排显示,显示 DataTables,行已经重新排列,以便 2 个表之间的任何匹配行都对齐。
我的 XAML 设置类似于 this question:
<DataGrid Name="comparisonGridLeft" ItemsSource="{Binding}" Margin="3" CanUserResizeRows="False">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Binding="{Binding Match}" Value="true">
<Setter Property="Background" Value="Red"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>
我的 DependencyProperty“匹配”定义类似于 this answer:
public class CustomProperties
{
public static DependencyProperty MatchProperty =
DependencyProperty.RegisterAttached("Match",
typeof(bool),
typeof(CustomProperties),
new PropertyMetadata(null));
public static void SetMatch(DependencyObject obj, bool value)
{
obj.SetValue(MatchProperty, value);
}
public static bool GetMatch(DependencyObject obj)
{
return (bool)(obj.GetValue(MatchProperty));
}
}
我的最后一个障碍是,当我遍历 DataGrids 以将“匹配”属性设置为正确的值时,我得到一个错误:
错误 CS1503:参数 1:无法从“System.Data.DataRowView”转换为“System.Windows.DependencyObject”
foreach (DataRowView leftRow in leftGrid.ItemsSource)
{
foreach (DataRowView rightRow in rightGrid.ItemsSource)
{
bool foundMatch = DetermineIfMatch(leftRow, rightRow);
if (foundMatch)
{
//Throws the compile-time error
CustomProperties.SetMatch(leftRow, true);
foundCloseMatch = true;
break;
}
}
}
提前感谢您的帮助。 WPF 新手,整天都在努力,但无济于事????
【问题讨论】: