1
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 属性,绑定正在为对象工作,总是为空。我怎样才能做到这一点?

4

1 回答 1

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) 
     {
           ....
     }

让我知道这是否有帮助...

于 2012-08-07T05:31:14.690 回答