0
私はthisチュートリアルを通してWPFを学習しており、私はlecture 20に固執しました。私はすべてのステップを追った。しかし、私のコードはアクセス方法RelativeSourceを使用したTextboxの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クラスが
0であるのに対し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