0
我正在通過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解決我的問題。
謝謝。
謝謝。有效。我已經嘗試了不同的組合,但我忘了這一個。再次感謝。 –
歡迎......... – WPFUser