我可以通过DataTrigger(XAML)调用函数吗

本文关键字:调用 函数 XAML 可以通过 DataTrigger | 更新日期: 2023-09-27 18:24:40

我在.xaml.cs中有一个函数,我想调用它:

.xaml.cs

private void treeView_SelectedItemChanged(object sender, EventArgs e)
{
        //treeView.ScrollToCenterOfView(sender as TreeViewItem);
}

喜欢这篇文章:Make ListView.ScrollIntoView将项目滚动到ListView的中心(C#)答案1

但我不使用SelectedItem,我的方法是:

XAML:

<Style.Triggers>
     <DataTrigger Binding="{Binding Path=IsActive}" Value="true">
          // ###call this function
     </DataTrigger>
</Style.Triggers>

我不知道如何通过改变数据模型来提高这个函数。在我看过的每一个教程中,他们都是通过事件/命令来完成的。

Thx

我可以通过DataTrigger(XAML)调用函数吗

只是为这些场景添加另一个稍微有点技巧的选项。

不是一个触发器,但您可以使用通用的FrameworkElement.Tag属性为要查看的属性设置绑定,然后在它更改时运行回调。

例如:

<DataTemplate>
    <StackPanel Tag="{Binding IsActive, NotifyOnTargetUpdated=True}"
                TargetUpdated="CodeBehindMethodToCall">
        <!-- snip rest of template --> 
    </StackPanel>
<DataTemplate>

这是我的解决方案,用于使用DataTrigger的Auto ScrollItemToCenter TreeView,因此它不是滚动的选定项目。

在XAML中,我通过DataTrigger 添加了一个自定义行为

                <Style TargetType="TreeViewItem" BasedOn="{StaticResource {x:Type TreeViewItem}}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsSelectedInGrid}" Value="true">
                            <Setter Property="local:TreeViewScrollBehavior.ScrollItemToCenter" Value="true" />
                        </DataTrigger>
                    </Style.Triggers>

我试着在这个简单的例子之后创建我自己的行为http://www.shujaat.net/2010/08/attached-behaviors.html

public static class TreeViewScrollBehavior
{
    public static DependencyProperty ScrollItemToCenterProperty =
DependencyProperty.RegisterAttached("ScrollItemToCenter", typeof(bool),
typeof(TreeViewScrollBehavior), new UIPropertyMetadata(false, OnScrollItemToCenter));

    public static bool GetScrollItemToCenter(DependencyObject obj)
    {
        return (bool)obj.GetValue(ScrollItemToCenterProperty);
    }
    public static void SetScrollItemToCenter(DependencyObject obj, bool value)
    {
        obj.SetValue(ScrollItemToCenterProperty, value);
    }
    public static void OnScrollItemToCenter(DependencyObject d,
        DependencyPropertyChangedEventArgs e)
    {
        if ((bool)e.NewValue)
        {
            //TreeViewItem t = GetTreeViewItem(d);
            TreeViewItem t = (TreeViewItem)d;
            TryScrollToCenterOfView(GetTree(t), t);
        }
    }

并增加了上述链路的略有变化的功能

    private static TreeView GetTree(TreeViewItem item)
    {
        var parent = VisualTreeHelper.GetParent(item);
        while (!(parent is TreeView))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }
        return parent as TreeView;
    }

    private static void TryScrollToCenterOfView(ItemsControl itemsControl, UIElement container)
    {
        // Find the container
        //var container =  itemsControl.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
        if (container == null) return;
        // Find the ScrollContentPresenter
        ScrollContentPresenter presenter = null;
        for (Visual vis = container; vis != null && vis != itemsControl; vis = VisualTreeHelper.GetParent(vis) as Visual)
            if ((presenter = vis as ScrollContentPresenter) != null)
                break;
        if (presenter == null) return;
        // Find the IScrollInfo
        var scrollInfo =
            !presenter.CanContentScroll ? presenter :
            presenter.Content as IScrollInfo ??
            FirstVisualChild(presenter.Content as ItemsPresenter) as IScrollInfo ??
            presenter;
        // Compute the center point of the container relative to the scrollInfo
        Size size = container.RenderSize;
        Point center = container.TransformToAncestor((Visual)scrollInfo).Transform(new Point(size.Width / 2, size.Height / 2));
        center.Y += scrollInfo.VerticalOffset;
        center.X += scrollInfo.HorizontalOffset;
        // Adjust for logical scrolling
        if (scrollInfo is StackPanel || scrollInfo is VirtualizingStackPanel)
        {
            double logicalCenter = itemsControl.ItemContainerGenerator.IndexFromContainer(container) + 0.5;
            Orientation orientation = scrollInfo is StackPanel ? ((StackPanel)scrollInfo).Orientation : ((VirtualizingStackPanel)scrollInfo).Orientation;
            if (orientation == Orientation.Horizontal)
                center.X = logicalCenter;
            else
                center.Y = logicalCenter;
        }
        // Scroll the center of the container to the center of the viewport
        if (scrollInfo.CanVerticallyScroll) scrollInfo.SetVerticalOffset(CenteringOffset(center.Y, scrollInfo.ViewportHeight, scrollInfo.ExtentHeight));
        if (scrollInfo.CanHorizontallyScroll) scrollInfo.SetHorizontalOffset(CenteringOffset(center.X, scrollInfo.ViewportWidth, scrollInfo.ExtentWidth));
    }
    private static double CenteringOffset(double center, double viewport, double extent)
    {
        return Math.Min(extent - viewport, Math.Max(0, center - viewport / 2));
    }
    private static DependencyObject FirstVisualChild(Visual visual)
    {
        if (visual == null) return null;
        if (VisualTreeHelper.GetChildrenCount(visual) == 0) return null;
        return VisualTreeHelper.GetChild(visual, 0);
    }

这是我在xaml.cs或DataObject中滚动不带代码的TreeView的完整解决方案。

Thx为用户行为的想法尴尬的编码器

如果有人有什么需要改进的地方,我愿意听听;-)

我想你不能。DataTrigger只是用来更改WPF级别的演示。标准方法是将控件提供的内容用作事件。

TreeViewItem类有一个IsSelected事件,更重要的是有一个Selected事件。因此,理论上,您可以在创建TreeViewItem时为其设置Selected事件的事件处理程序。

希望很明显,这在MVVM secenario中不起作用,因为您无法访问创建的TreeViewItem实例,在这种情况下,我希望使用TreeView控件的Behavior来实现这一点。