To jak ja powielana tego problemu w WPF:TemplateBinding nie działa w niektórych przypadkach (przy użyciu TranslateTransform)
utworzyć formant niestandardowy:
public class TestCustomControl : Control
{
static TestCustomControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(TestCustomControl), new FrameworkPropertyMetadata(typeof(TestCustomControl)));
}
public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(TestCustomControl), new PropertyMetadata("Hello"));
public double OffSet
{
get { return (double)GetValue(OffSetProperty); }
set { SetValue(OffSetProperty, value); }
}
// Using a DependencyProperty as the backing store for OffSet. This enables animation, styling, binding, etc...
public static readonly DependencyProperty OffSetProperty =
DependencyProperty.Register("OffSet", typeof(double), typeof(TestCustomControl), new PropertyMetadata(5.0));
}
dodać styl do niej w generycznych. plik XAML:
<Style TargetType="local:TestCustomControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:TestCustomControl">
<Grid>
<TextBlock Text="{TemplateBinding Text}"></TextBlock>
<TextBlock Text="{TemplateBinding Text}">
<TextBlock.RenderTransform>
<TranslateTransform X="{TemplateBinding OffSet}" Y="{TemplateBinding OffSet}"/>
<!--<TranslateTransform X="10" Y="10"/>-->
</TextBlock.RenderTransform>
</TextBlock>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
następnie dodać go do głównego okna:
<local:TestCustomControl OffSet="32" Text="the off set is not working" FontSize="36">
</local:TestCustomControl>
Następnie uruchom aplikację, okazało się, że "Tekst" działa dobrze, ale "OffSet" nie działa. I próbowałem podobnej rzeczy w środowisku programistycznym Windows Phone 7, i mam ten sam wynik.
Jak zmodyfikować kod, aby działał OffSet?
Dzięki
Zgodnie z "WPF 4.5 Unleashed", Nathan, Adam; 3 ed. do. 2014, s. 437, nie możesz użyć 'TemplateBinding' na właściwościach' Freezeable'. 'TranslateTransform' to' Freezeable', dlatego nie działało (podczas gdy 'TextBlock' nie jest' Freezeable', dlatego właśnie działało). Zastanawiające jest to, że nie jest to błąd w czasie wykonywania (lub jakikolwiek inny czas). To po prostu zawiesza się w milczeniu. –