none
DocumentViewer如何设置工具栏按钮状态? RRS feed

  • 问题

  • 各位大神,请教一个DocumentViewer工具栏按钮状态的问题

    前台代码:

     <DocumentViewer x:Name="viewer"   />

    这样加载出来的按钮未选中任何按钮

    当后台代码调用适应页宽的方法

    viewer.FitToWidth();

    文档确实是适应页宽了,可是按钮状态却没有改变

    而手动点击按钮有一个选中的状态,现在我希望后台代码调用的时候,也能有这个选中状态,该如何实现?

    2018年10月25日 13:19

答案

  • Hi    dk.huang,

    你可以通过查找Toolbar中的对应button。 然后设置选中状态。

     public void SetselectButton()
            {
                foreach (Button uie in FindVisualChildren<Button>(viewer))
                {
                    if (uie.Name == "PageWidthButton")
                    {
                       
                        uie.Focus();
                    }
                }
            }
            public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            yield return (T)child;
                        }
    
                        foreach (T childOfChild in FindVisualChildren<T>(child))
                        {
                            yield return childOfChild;
                        }
                    }
                }
            }
    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年10月26日 9:40
    版主

全部回复

  • 已经解决了,通过Template.FindName找到对应的按钮,设置Focus()即可
    2018年10月26日 9:36
  • Hi    dk.huang,

    你可以通过查找Toolbar中的对应button。 然后设置选中状态。

     public void SetselectButton()
            {
                foreach (Button uie in FindVisualChildren<Button>(viewer))
                {
                    if (uie.Name == "PageWidthButton")
                    {
                       
                        uie.Focus();
                    }
                }
            }
            public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
            {
                if (depObj != null)
                {
                    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                    {
                        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                        if (child != null && child is T)
                        {
                            yield return (T)child;
                        }
    
                        foreach (T childOfChild in FindVisualChildren<T>(child))
                        {
                            yield return childOfChild;
                        }
                    }
                }
            }
    Best Regards,

    Yong Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年10月26日 9:40
    版主