如何使用RelativeSource访问文本框MultiBuinding中的附加属性

问题描述:

我正在通过this教程学习WPF,并且我卡在lecture 20上。我跟着亦步亦趋,但我的代码提供错误如何使用RelativeSource访问文本框MultiBuinding中的附加属性

嵌套类型不支持:Canvas.Top

问题是,它成功地在讲课视频

我的XAML代码运行是:

<Window.Resources> 
    <local:ThresholdRuleConverter x:Key="ruleConverter" /> 
</Window.Resources> 
<StackPanel> 

    <TextBox x:Name="txtAmount" Text="{Binding Path=ItemAmount}" 
      HorizontalAlignment="Stretch" 
      Tag="{Binding Path=ItemAmount, Mode=OneTime}" Height="35" FontSize="22" 
      Canvas.Top="{Binding Path=Threshold}"> 
     <TextBox.Background> 
      <MultiBinding Converter="{StaticResource ruleConverter}" ConverterParameter="Red,Yellow,Green"> 
       <Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="Tag" /> 
       <Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="{Canvas.Top}" /> 
       <Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="Text" /> 
      </MultiBinding> 
     </TextBox.Background> 
    </TextBox> 

</StackPanel> 

而我ThresholdRuleConverter类是

public class ThresholdRuleConverter : IMultiValueConverter 
{ 
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) 
    { 
     //define base colors 
     SolidColorBrush invalidBrush = new SolidColorBrush(Colors.Red); 
     SolidColorBrush equalBrush = new SolidColorBrush(Colors.Yellow); 
     SolidColorBrush validBrush = new SolidColorBrush(Colors.Green); 

     if (parameter != null) 
     { 
      string[] definedColors = parameter.ToString().Split(','); 
      BrushConverter converter = new BrushConverter(); 
      if (definedColors.Length > 0) 
      { 
       invalidBrush = converter.ConvertFromString(definedColors[0]) as SolidColorBrush; 
       if (definedColors.Length > 1) 
        equalBrush = converter.ConvertFromString(definedColors[1]) as SolidColorBrush; 
       if (definedColors.Length > 2) 
        validBrush = converter.ConvertFromString(definedColors[2]) as SolidColorBrush; 
      } 
     } 
     if (values.Length < 3) 
      return invalidBrush; 

     try 
     { 
      if (values[0] != DependencyProperty.UnsetValue && values[1] != DependencyProperty.UnsetValue 
       && values[2] != DependencyProperty.UnsetValue) 
      { 
       int oldValue = System.Convert.ToInt32(values[0]); 
       int thresholdValue = System.Convert.ToInt32(values[1]); 
       int newValue = System.Convert.ToInt32(values[2]); 

       if (newValue > oldValue && (newValue - oldValue) <= thresholdValue) 
        return validBrush; 
       else if (newValue == oldValue) 
        return equalBrush; 
       else 
        return invalidBrush; 
      } 
      return invalidBrush; 
     } 
     catch (Exception) 
     { 
      return invalidBrush; 
     } 
    } 

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 

和我简单txtAmount的DataContext是

txtAmount.DataContext = new Widget { ItemAmount = 25, Threshold = 50 }; 

的错误是发生在路径第二结合

<Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="{Canvas.Top}" /> 

谁能告诉我我怎么可以参考帆布。在上述场景中的路径顶部

的一个解决办法是,我可以直接使用此:

<Binding Mode="OneWay" Path="Threshold" /> 

但我想用Canvas.Top解决我的问题。

谢谢。

,而不是这个,

<Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="{Canvas.Top}" /> 

使用这样的,

<Binding Mode="OneWay" RelativeSource="{RelativeSource Self}" Path="(Canvas.Top)" /> 

大括号取代。

+0

谢谢。有效。我已经尝试了不同的组合,但我忘了这一个。再次感谢。 –

+0

欢迎......... – WPFUser