【问题标题】:Binding from an XAML resource dictionary to a class property从 XAML 资源字典绑定到类属性
【发布时间】:2012-04-02 00:00:52
【问题描述】:

我很难设置我认为应该很容易的绑定。非常感谢您的帮助。

我有一个名为 FormResource.xaml 的资源字典。在这本字典中包含我重新编写模板的 ScrollView 的样式。目的是我想要一个更宽的垂直滚动条。

<Style x:Key="LargeScrolling" TargetType="ScrollViewer">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ScrollViewer">
                <Grid Background="{TemplateBinding Background}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <ScrollContentPresenter x:Name="ScrollContentPresenter"
                            Margin="{TemplateBinding Padding}"
                            ContentTemplate="{TemplateBinding ContentTemplate}"/>
                    <ScrollBar x:Name="PART_VerticalScrollBar" 
                            Style="{StaticResource LargeVerticalScrollBar}" 
                            Width="{Binding ElementName=MDTForm, Path=ScrollBarWidth}"
                            IsTabStop="False"
                            Visibility="{TemplateBinding ComputedVerticalScrollBarVisibility}"
                            Grid.Column="1" Grid.Row="0" Orientation="Vertical"
                            ViewportSize="{TemplateBinding ViewportHeight}"
                            Maximum="{TemplateBinding ScrollableHeight}"
                            Minimum="0"
                            Value="{TemplateBinding VerticalOffset}"
                            Margin="0,-1,-1,-1"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我有一个名为 FormControl 的用户控件。

public class FormControl : UserControl

我曾经把它作为一个带有 XAML 组件的部分类,我试图在其中工作,但我不得不删除 XAML,因为我从另一个程序集中的这个类派生,而 WPF 不允许你从另一个程序集中的部分类派生。

在 FormControl 中,我定义了一个 ScrollBarWidth 属性。

    public static readonly DependencyProperty ScrollBarWidthProperty = DependencyProperty.Register("ScrollBarWidth", typeof(double), typeof(FormControl));
    public double ScrollBarWidth
    {
        get { return (double)base.GetValue(ScrollBarWidthProperty); }
        set { base.SetValue(ScrollBarWidthProperty, value); }
    }

当我在主声明中将此作为部分类时,我为 FormControl 类指定了 MDTForm 的名称,这就是我在绑定中用作 ElementName 的名称。我尝试在 FormClass.cs 中注册此名称,但无论我做什么,滚动条都不会获取属性值。

这里是我在 FormControl 类中创建 ScrollViewer 的地方。

        _canvasScrollViewer = new ScrollViewer();
        _canvasScrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
        _canvasScrollViewer.VerticalAlignment = VerticalAlignment.Top;
        _canvasScrollViewer.MaxHeight = Constants.ScrollViewMaxHeight;
        _canvasScrollViewer.Style = (Style)FindResource("LargeScrolling");

我让它工作的唯一方法是绑定到一个静态属性。我用这个来装订。

Width="{Binding Source={x:Static form:FormControl.ScrollBarWidthP}}"

然后这样定义属性。

public static double ScrollBarWidth { get; set; }

但是,我不希望这样,因为我可以同时加载多个 FormControl 对象,而且它们可能不都具有相同的滚动条宽度属性。

【问题讨论】:

    标签: wpf xaml binding resources properties


    【解决方案1】:

    使用 RelativeSource 绑定而不是 ElementName:

    {Binding RelativeSource={RelativeSource Mode=FindAncestor,
             AncestorType={x:Type controls:FormControl}}, Path=ScrollBarWidth}
    

    这将在运行时沿着可视化树向上查找包含 ScrollViewer 的父控件,这解决了范围界定和多实例问题。

    【讨论】:

    • 谢谢,这太棒了,而且效果很好。是否可以让它支持自动属性而不是 DependancyProperty?它只需要单向绑定,ScrollViewer 永远不会更新 FormControl 类的属性。它现在可以工作了,这很好,但如果可能的话,我宁愿不必使用 DependancyProperty。
    • 如果您为它分配一个绑定(在本例中为宽度),它只需要是一个 DP,而不是在另一边 - 这对于双向绑定也是如此。在这种情况下,ScrollBarWidth 可以是 DP、使用 INotifyPropertyChanged 的​​属性(如果对其的更新需要显示在 Width 上),或者如果不需要更新,则可以是任何普通属性。
    • 当我将其更改为在 public double ScrollBarWidth { get; 中定义的类属性时放; } 它不起作用。我没有对 Binding 语句进行任何更改,因此可能需要进行更改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    • 1970-01-01
    • 2014-11-12
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2017-08-28
    相关资源
    最近更新 更多