0
this 튜토리얼을 통해 WPF를 배우고 있으며 lecture 20에 머물러 있습니다. 나는 모든 단계를 따라하지만 내 코드는RelativeSource를 사용하여 Textbox의 MultiBuinding에서 연결된 속성
중첩 유형은 지원되지 않습니다 오류 제공 : Canvas.Top을
요점은,이 강의 비디오내 XAML 코드에서 성공적으로 실행됩니다
입니다 : 내 ThresholdRuleConverter 클래스 반면
<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>
는
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