简体   繁体   English

未从样式设置依赖项属性

[英]Dependency Property not set from style

I'm trying to synchronize scrolling between two datagrids so that each scroll is mirrored between them (either horizontal or vertical scrolling), after googling around how to do I started to implement my method but the setter call from my scrollbar style never calls the dependency object to set the value. 我试图同步两个数据网格之间的滚动,以便每个滚动都在它们之间进行镜像(水平滚动或垂直滚动​​),在谷歌搜索如何做之后,我开始实现我的方法,但是从滚动条样式调用的setter从未调用依赖项设置值的对象。

This is my data grid. 这是我的数据网格。

<dataGridEx:DataGridEx ColumnHeaders="{Binding SystemMonitorValues.ColumnHeaders}"
                       ItemsSource="{Binding SystemMonitorValues.Rows}"
                       Style="{StaticResource DataGridStyle}"
                       ScrollViewer.CanContentScroll="True"
                       ScrollViewer.VerticalScrollBarVisibility="Auto"
                       ScrollViewer.HorizontalScrollBarVisibility="Auto">
    <dataGridEx:DataGridEx.Resources>
        <Style TargetType="{x:Type ScrollBar}">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="scroll:ScrollSynchronizer.ScrollGroup" Value="Group1" />
        </Style>
    </dataGridEx:DataGridEx.Resources>
</dataGridEx:DataGridEx>

So within the scroll bar style I am attempting to set the ScrollSynchronizer.ScrollGroup to have the value "Group1". 因此,在滚动条样式中,我尝试将ScrollSynchronizer.ScrollGroup设置为值“ Group1”。

My ScrollSynchronizer is setup as follows: 我的ScrollSynchronizer设置如下:

public class ScrollSynchronizer : DependencyObject
{
    public static readonly DependencyProperty ScrollGroupProperty = DependencyProperty.Register(@"ScrollGroup",
            typeof(string), typeof(ScrollSynchronizer), new PropertyMetadata(new PropertyChangedCallback(OnScrollGroupChanged)));

static ScrollSynchronizer()
{
}

public string ScrollGroup
{
    get
    {
        return (string)this.GetValue(ScrollGroupProperty);
    }

    set
    {
        this.SetValue(ScrollGroupProperty, value);
    }
}

private static void OnScrollGroupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    var scrollViewer = d as System.Windows.Controls.ScrollViewer;

    ...
}

I'm placing a breakpoint within the OnScrollGroupChanged method which is the PropertyChangedCallback for the DependencyProperty but for some reason this is never hit. 我在OnScrollGroupChanged方法中放置了一个断点,该方法是DependencyPropertyPropertyChangedCallback ,但由于某种原因,它从未实现。

I know the style is working as the background of the scrollbar is being set to Red but the setter for the ScrollGroup doesn't seem to want to be called, this is also shown in Snoop in that the Style is correctly set with the two setters and even the setter for the ScrollSynchronizer point's to the correct object. 我知道样式正在工作,因为滚动条的背景被设置为Red但是ScrollGroup的设置器似乎不想被调用,这在Snoop中也显示出来,因为已使用两个设置器正确设置了样式甚至ScrollSynchronizer点的设置器ScrollSynchronizer指向正确的对象。

I'm simply at a loss to why this is not being set. 我完全不知道为什么没有设置它。

ScrollSynchronizer.ScrollGroup should be an attached property instead of a regular dependency property: ScrollSynchronizer.ScrollGroup应该是附加属性,而不是常规依赖项属性:

public static class ScrollSynchronizer
{
    public static readonly DependencyProperty ScrollGroupProperty =
        DependencyProperty.RegisterAttached(
            "ScrollGroup", typeof(string), typeof(ScrollSynchronizer),
            new PropertyMetadata(OnScrollGroupChanged));

    public static string GetScrollGroup(DependencyObject obj)
    {
        return (string)obj.GetValue(ScrollGroupProperty);
    }

    public static void SetScrollGroup(DependencyObject obj, string value)
    {
        obj.SetValue(ScrollGroupProperty, value);
    }

    private static void OnScrollGroupChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var scrollBar = d as ScrollBar;
        ...
    }
}

Note also that the DependencyObject parameter of the PropertyChangedCallback is of type ScrollBar when you set the property in a ScrollBar Style. 还要注意的是,PropertyChangedCallback的DependencyObject的参数类型的ScrollBar ,当你设置滚动条样式属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM