如何使用Generic.xaml中的样式将值从usercontrol传递到customcontrol?

时间:2014-12-19 19:40:40

标签: c# wpf xaml user-controls custom-controls

在尝试创建一个接受字符串(Text)的简单自定义控件时,我很难通过Generic.xaml中的样式将值从XAML传递到customcontrol。

调用XAML:

<wc:ccTestFigure Text="{Binding Text, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}" />

ccTestFigure定义为:

 public class ccTestFigure : FrameworkElement
{

    public static readonly DependencyProperty TextProperty =
        TextBlock.TextProperty.AddOwner(typeof(ccTestFigure));

    public String Text
    {
        get { return (String)this.GetValue(TextProperty); }
        set { this.SetValue(TextProperty, value); }
    }

    static ccTestFigure()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(ccTestFigure), new FrameworkPropertyMetadata(typeof(ccTestFigure)));
    }

    public ccTestFigure()
    {

        var typeface = new Typeface(
                        FontFamily,
                        FontStyle,
                        FontWeights.Normal,
                        FontStretches.Normal);

       ft  = new FormattedText(
               Text,
               System.Threading.Thread.CurrentThread.CurrentCulture,
               FlowDirection.LeftToRight,
               typeface,
               FontSize,
               Foreground);
    }


  protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);
        drawingContext.DrawText(ft, new Point());
    }

Generic.xaml中的样式不喜欢TemplateBinding,所以我不知道如何将用户控件中的Text in传递给customcontrol,ccTestFigure。

我到目前为止的风格(不起作用)是:

 <Style TargetType="{x:Type local:ccTestFigure}">
    <Setter Property="Text" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>
</Style>

Text是一个简单的字符串。

我感谢任何帮助。提前谢谢。

2 个答案:

答案 0 :(得分:1)

看起来你正在尝试做一些完全冗余的事情,将“Text”的值设置为自身。

问题只是当“文字”改变时你没有更新“ft”。添加一个属性更改处理程序,并将格式化文本内容放在那里而不是构造函数:

public static readonly DependencyProperty TextProperty =
    TextBlock.TextProperty.AddOwner(typeof(ccTestFigure), new FrameworkPropertyMetadata(propertyChangedCallback: OnTextChanged));

private static void OnTextChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
    ((ccTestFigure)sender).UpdateText();
}

private void UpdateText() 
{
    var typeface = new Typeface(
                    FontFamily,
                    FontStyle,
                    FontWeights.Normal,
                    FontStretches.Normal);

   ft  = new FormattedText(
           Text,
           System.Threading.Thread.CurrentThread.CurrentCulture,
           FlowDirection.LeftToRight,
           typeface,
           FontSize,
           Foreground);
}

public ccTestFigure()
{
}

答案 1 :(得分:1)

我发布这个,因为我知道没有其他方式来显示代码。 McGamagle是正确的 - 只需要进行如下的一些更改。需要添加&#34; AffectsRender&#34;显示文字。

  public static readonly DependencyProperty TextProperty =
        TextBlock.TextProperty.AddOwner(typeof(ccTextFigure),
         new FrameworkPropertyMetadata(
            null,                                               
            FrameworkPropertyMetadataOptions.AffectsRender | FrameworkPropertyMetadataOptions.AffectsMeasure,    
            propertyChangedCallback: OnTextChanged              
            ));

在我的情况下,我还需要AffectsMeasure强制重新测量父滚动查看器。