Wpf 无法从datatemplate访问Datagrid datacontext
然后Wpf 无法从datatemplate访问Datagrid datacontext,wpf,wpfdatagrid,Wpf,Wpfdatagrid,然后 DataGrid ItemsSource="{Binding Legs}" Grid.Row="1" DataContext="{Binding}" Tag="{Binding}" CanUserAddRows="False" CanUse
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}"
>
在上面的XAML代码中。。。我正在尝试访问datagrid的标记属性,对象的绑定始终为空。我怎样才能做到这一点 问题从您的代码中显而易见
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");
}
}
现在,您的转换器接收到它需要的所有3个值
<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>
让我知道这是否有帮助…从您的代码中可以看出问题
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");
}
}
现在,您的转换器接收到它需要的所有3个值
<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>
请告诉我这是否有帮助…不要使用
标记属性。这是糟糕的风格。改用DataContext。是您的DealObject。Deal
是一个从属属性
?显示您的DealObject
的描述代码。确定还包括我的转换器代码。谢谢你的标签建议。。。但它不起作用。传递给转换器的对象为null。我认为应该在这里描述您的根本问题。你应该写下你想做什么,并展示出来。我想这样社区就可以找到更好的解决方案。不要使用标签
属性。这是糟糕的风格。改用DataContext。是您的DealObject。Deal
是一个从属属性
?显示您的DealObject
的描述代码。确定还包括我的转换器代码。谢谢你的标签建议。。。但它不起作用。传递给转换器的对象为null。我认为应该在这里描述您的根本问题。你应该写下你想做什么,并展示出来。我想这样社区就可以找到更好的解决方案了。没错,我不需要在converter对象上进行适当的处理,我应该使用多重绑定本身。谢谢你的帮助。但要理解的是,在这种情况下有哪些选择。例如,我甚至尝试创建一个框架元素作为网格资源,并使用它作为绑定的静态重选,类似于和我很抱歉;我的错。。。上面的隧道确实管用。我犯了一个错误;我的错。非常感谢WPF-IT:)只要元素位于可视树(甚至不是逻辑树)中,任何选项都可以。这些资源应该是可视化树的一部分,这样它们才能正常工作。谢谢你的帮助。但要理解的是,在这种情况下有哪些选择。例如,我甚至尝试创建一个框架元素作为网格资源,并使用它作为绑定的静态重选,类似于和我很抱歉;我的错。。。上面的隧道确实管用。我犯了一个错误;我的错。非常感谢WPF-IT:)只要元素位于可视树(甚至不是逻辑树)中,任何选项都可以。这些资源应该是可视化树的一部分,这样它们才能正常工作。
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)
{
....
}