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>
以下是我在转换器上的Depenency属性
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属性,该对象的绑定工作总是为null。我怎样才能做到这一点?
答案 0 :(得分:1)
问题在你的代码中很明显......
<Control:SwapswireStatusImagePathConvertor.DealProp>
<Control:DealObject
Deal="{Binding RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}},
Path=Tag}"/>
</Control:SwapswireStatusImagePathConvertor.DealProp>**
您的转换器或转换器中的任何属性永远不会成为可视树的一部分,因此即使您的交易对象或转换器本身是DependencyObject
,绑定也不会对其起作用 !
请修改你的绑定......
为什么您的转换器需要具有需要绑定的属性?您已经拥有MultiBinding
,因此请将此绑定作为其中的一部分....
<MultiBinding>
<Binding Path="SwapswireBuyerStatusText"/>
<Binding Path="SwapswireSellerStatusText"/>
**<Binding RelativeSource="{RelativeSource FindAncestor,
AncestorType={x:Type DataGrid}}"
Path="Tag"/>**
<MultiBinding.Converter>
<Control:SwapswireStatusImagePathConvertor
AllGoodPath="/Resources/Done.png"
InProgressPath="/Resources/Go.png"
WarningPath="/Resources/Warning.png" />
</MultiBinding.Converter>
</MultiBinding>
现在您的转换器会收到它需要的所有3个值...
String str1 = System.Convert.ToString(values[0]);
String str2 = System.Convert.ToString(values[1]);
** this.DealProp = new DealObject();
this.DealProp.Deal = values[2] as CMBSTrade; **
if (DealProp.Deal.Swapswire)
{
....
}
如果这有帮助,请告诉我......