设置绑定到模板子项

时间:2013-02-20 09:07:25

标签: c# wpf templates binding frameworkelementfactory

我从System.Windows.Controls.ScrollViewer派生并更改了模板;我的想法是创建一个ScrollViewer,如Visual Studio 2010中所见,它有一个小的ComboBox来将缩放设置为文本。

ZoomPercentageValueComboBox”包含以下DependencyProperty

public double ZoomValue
{
    get { return (double)GetValue(ZoomValueProperty); }
    set { SetValue(ZoomValueProperty, value); }
}

public static readonly DependencyProperty ZoomValueProperty =
    DependencyProperty.Register("ZoomValue", typeof(double),
    typeof(ZoomPercentageComboBox), new FrameworkPropertyMetadata(1D, FrameworkPropertyMetadataOptions.Journal,
        new PropertyChangedCallback(ZoomValue_PropertyChanged)));

private static void ZoomValue_PropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs dpcea)
{
    ((ZoomPercentageComboBox)dpo).Text = (((double)dpcea.NewValue) * 100).ToString() + " %";
}

新的ScrollViewer有以下方法:

private static ControlTemplate CreateDefaultControlTemplate()
{
    #region DEFAULT
    ControlTemplate template = null;
    FrameworkElementFactory factory = new FrameworkElementFactory(typeof(Grid), "Grid");
    FrameworkElementFactory child = new FrameworkElementFactory(typeof(ColumnDefinition), "ColumnDefinitionOne");
    FrameworkElementFactory factory3 = new FrameworkElementFactory(typeof(ColumnDefinition), "ColumnDefinitionTwo");
    FrameworkElementFactory factory4 = new FrameworkElementFactory(typeof(RowDefinition), "RowDefinitionOne");
    FrameworkElementFactory factory5 = new FrameworkElementFactory(typeof(RowDefinition), "RowDefinitionTwo");
    FrameworkElementFactory factory6 = new FrameworkElementFactory(typeof(ScrollBar), "PART_VerticalScrollBar");
    FrameworkElementFactory factory7 = new FrameworkElementFactory(typeof(ScrollBar), "PART_HorizontalScrollBar");
    FrameworkElementFactory factory8 = new FrameworkElementFactory(typeof(ScrollContentPresenter), "PART_ScrollContentPresenter");
    FrameworkElementFactory factory9 = new FrameworkElementFactory(typeof(Rectangle), "Corner");
    Binding binding = new Binding("HorizontalOffset")
    {
        Mode = BindingMode.OneWay,
        RelativeSource = RelativeSource.TemplatedParent
    };
    Binding binding2 = new Binding("VerticalOffset")
    {
        Mode = BindingMode.OneWay,
            RelativeSource = RelativeSource.TemplatedParent
    };
    factory.SetValue(Panel.BackgroundProperty, new TemplateBindingExtension(Control.BackgroundProperty));
    factory.AppendChild(child);
    factory.AppendChild(factory3);
    factory.AppendChild(factory4);
    factory.AppendChild(factory5);
    factory.AppendChild(factory9);
    factory.AppendChild(factory8);
    factory.AppendChild(factory6);
    factory.AppendChild(factory7);
    child.SetValue(ColumnDefinition.WidthProperty, new GridLength(1.0, GridUnitType.Star));
    factory3.SetValue(ColumnDefinition.WidthProperty, new GridLength(1.0, GridUnitType.Auto));
    factory4.SetValue(RowDefinition.HeightProperty, new GridLength(1.0, GridUnitType.Star));
    factory5.SetValue(RowDefinition.HeightProperty, new GridLength(1.0, GridUnitType.Auto));
    factory8.SetValue(Grid.ColumnProperty, 0);
    factory8.SetValue(Grid.RowProperty, 0);
    factory8.SetValue(FrameworkElement.MarginProperty, new TemplateBindingExtension(Control.PaddingProperty));
    factory8.SetValue(ContentControl.ContentProperty, new TemplateBindingExtension(ContentControl.ContentProperty));
    factory8.SetValue(ContentControl.ContentTemplateProperty, new TemplateBindingExtension(ContentControl.ContentTemplateProperty));
    factory8.SetValue(CanContentScrollProperty, new TemplateBindingExtension(CanContentScrollProperty));
    factory7.SetValue(ScrollBar.OrientationProperty, Orientation.Horizontal);
    factory7.SetValue(Grid.ColumnProperty, 0);
    factory7.SetValue(Grid.RowProperty, 1);
    factory7.SetValue(RangeBase.MinimumProperty, 0.0);
    factory7.SetValue(RangeBase.MaximumProperty, new TemplateBindingExtension(ScrollableWidthProperty));
    factory7.SetValue(ScrollBar.ViewportSizeProperty, new TemplateBindingExtension(ViewportWidthProperty));
    factory7.SetBinding(RangeBase.ValueProperty, binding);
    factory7.SetValue(UIElement.VisibilityProperty, new Te emplateBindingExtension(ComputedHorizontalScrollBarVisibilityProperty));
    factory7.SetValue(FrameworkElement.CursorProperty, Cursors.Arrow);
    factory7.SetValue(AutomationProperties.AutomationIdProperty, "HorizontalScrollBar");
    factory6.SetValue(Grid.ColumnProperty, 1);
    factory6.SetValue(Grid.RowProperty, 0);
    factory6.SetValue(RangeBase.MinimumProperty, 0.0);
    factory6.SetValue(RangeBase.MaximumProperty, new TemplateBindingExtension(ScrollableHeightProperty));
    factory6.SetValue(ScrollBar.ViewportSizeProperty, new TemplateBindingExtension(ViewportHeightProperty));
    factory6.SetBinding(RangeBase.ValueProperty, binding2);
    factory6.SetValue(UIElement.VisibilityProperty, new TemplateBindingExtension(ComputedVerticalScrollBarVisibilityProperty));
    factory6.SetValue(FrameworkElement.CursorProperty, Cursors.Arrow);
    factory6.SetValue(AutomationProperties.AutomationIdProperty, "VerticalScrollBar");
    factory9.SetValue(Grid.ColumnProperty, 1);
    factory9.SetValue(Grid.RowProperty, 1);
    factory9.SetResourceReference(Shape.FillProperty, SystemColors.ControlBrushKey);

    #endregion

    /* *** */
    factory7.SetValue(MarginProperty, new Thickness(ZoomBoxWidth, 0, 0, 0));
    /* *** */
    FrameworkElementFactory factory10 = new FrameworkElementFactory(typeof(ZoomPercentageComboBox), ZoomBoxTemplateName);
    factory.AppendChild(factory10);
    factory10.SetValue(Grid.ColumnProperty, 0);
    factory10.SetValue(Grid.RowProperty, 1);
    factory10.SetValue(ZoomPercentageComboBox.HorizontalAlignmentProperty, 
        HorizontalAlignment.Left);
    factory10.SetValue(ZoomPercentageComboBox.HeightProperty, 17D);
    factory10.SetValue(ZoomPercentageComboBox.WidthProperty, ZoomBoxWidth);
    factory10.SetValue(ZoomPercentageComboBox.IsEditableProperty, true);
    factory10.SetValue(ZoomPercentageComboBox.VerticalContentAlignmentProperty, VerticalAlignment.Top);
    factory10.SetValue(ZoomPercentageComboBox.HorizontalContentAlignmentProperty, HorizontalAlignment.Left);
    factory10.SetValue(ZoomPercentageComboBox.FontSizeProperty, 10D);
    factory10.SetValue(ZoomPercentageComboBox.FontFamilyProperty, new FontFamily("Arial"));
    factory10.SetValue(ZoomPercentageComboBox.BorderBrushProperty, null);
    factory10.SetValue(ZoomPercentageComboBox.BorderThicknessProperty, new Thickness(0));

    /* **************************************************************
???
    */

    template = new ControlTemplate(typeof(LSArbeitscanvasScrollViewer))
    {
        VisualTree = factory
    };
    template.Seal();

    return template;
}

这实际上取自原来的ScrollViewer。现在我想确保如果ComboBox中的“ZoomValueProperty”发生更改,DependencyPropertyZoomPercentageValueProperty”(ScrollViewer的类成员)会发生变化,太。我有这样的想法:

Binding binding = new Binding("HorizontalOffset")
    {
        Mode = BindingMode.OneWay,
        RelativeSource = RelativeSource.TemplatedParent
    };

但这并没有真正起作用

1 个答案:

答案 0 :(得分:0)

我终于通过在ComboBox类中使用RoutedEvent并检查事件的源是模板化的ComboBox来解决这个问题:

private void ZoomPercentageComboBox_ZoomValueChanged(object sender, ZoomValueChangedEventArgs e)
{
    if (e.Source == templatedZoomPercentageComboBox)
        ZoomPercentageComboBoxZoomPercentageValue = ((double)e.NewValue);
}

private const string ZoomBoxTemplateName = "PART_ZoomBox";

private ZoomPercentageComboBox templatedZoomPercentageComboBox
{
    get
    {
        return this.GetTemplateChild(ZoomBoxTemplateName) as ZoomPercentageComboBox;
    }
}