DataGrid ItemsSource="{Binding Legs}" Grid.Row="1"
DataContext="{Binding}"
Tag="{Binding}"
CanUserAddRows="False"
CanUserDeleteRows="False"
HorizontalAlignment="Stretch"
IsSynchronizedWithCurrentItem="True"
x:Name="statusGrid"
AutoGenerateColumns="False" HorizontalScrollBarVisibility="Hidden"
RowStyle="{StaticResource GridRowStyle}"
>
接着
<DataGrid.Columns>
<DataGridTemplateColumn Width="Auto" Header="Status">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Height="16" MaxHeight="14" Width="14" MaxWidth="14" HorizontalAlignment="Center">
<Image.Source>
<MultiBinding>
<Binding Path="SwapswireBuyerStatusText"/>
<Binding Path="SwapswireSellerStatusText"/>
<MultiBinding.Converter>
<Control:SwapswireStatusImagePathConvertor AllGoodPath="/Resources/Done.png"
InProgressPath="/Resources/Go.png" WarningPath="/Resources/Warning.png">
**<Control:SwapswireStatusImagePathConvertor.DealProp>
<Control:DealObject Deal="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}, Path=Tag}"/>
</Control:SwapswireStatusImagePathConvertor.DealProp>**
</Control:SwapswireStatusImagePathConvertor>
</MultiBinding.Converter>
</MultiBinding>
</Image.Source>
</Image>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
以下是我在转换器上的依赖属性
public class DealObject : DependencyObject
{
public static DependencyProperty TradedDecimalsProperty =
DependencyProperty.Register("TradedDecimals", typeof(Int32), typeof(DealObject), new UIPropertyMetadata(0));
public static DependencyProperty DealProperty =
DependencyProperty.Register("Deal", typeof(CMBSTrade), typeof(DealObject), new UIPropertyMetadata(new CMBSTrade()));
public CMBSTrade Deal
{
get { return (CMBSTrade)GetValue(DealProperty); }
set { SetValue(DealProperty, value); }
}
public Int32 TradedDecimals
{
get { return (Int32)GetValue(TradedDecimalsProperty); }
set { SetValue(TradedDecimalsProperty, value); }
}
}
[ValueConversion(typeof(object), typeof(BitmapImage))]
public class SwapswireStatusImagePathConvertor : IMultiValueConverter
{
public String AllGoodPath { get; set; }
public String InProgressPath { get; set; }
public String WarningPath { get; set; }
private DealObject _DealProp = null;
public DealObject DealProp
{
get { return _DealProp; }
set { _DealProp = value; }
}
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
String path = WarningPath;
try
{
if (null != DealProp && null != DealProp.Deal && null != values && values.Length == 2)
{
String str1 = System.Convert.ToString(values[0]);
String str2 = System.Convert.ToString(values[1]);
if (DealProp.Deal.Swapswire)
{
switch (MBSConfirmationHelper.GetSwapswireStatus(str1, str2, MBSConfirmationHelper.GetParticipantType(DealProp.Deal)))
{
case DealExecutionStatus.InProgress:
path = InProgressPath; break;
case DealExecutionStatus.ActionRequired:
case DealExecutionStatus.Error:
path = WarningPath;
break;
case DealExecutionStatus.Executed:
path = AllGoodPath;
break;
case DealExecutionStatus.NotApplicable:
path = String.Empty;
break;
}
}
else path = String.Empty;
}
}
catch (Exception)
{
}
return String.IsNullOrEmpty(path)? null : new BitmapImage(new Uri(path, UriKind.Relative));
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("Not Implemented");
}
}
在上面的 XAML 代码中......我正在尝试访问 datagrid 的 Tag 属性,绑定正在为对象工作,总是为空。我怎样才能做到这一点?