Using WPF expander control on microsoft surface platform

限于喜欢 提交于 2019-12-10 23:03:24

问题


I am trying to use the Expander control in the surface application. I have seen its not a surface control so application compiles and control shows up but the contacts are not working.

Is there anyway I can modify the contact events and make it work in surface applications.


回答1:


To do that, all you have to do is change the Expander's template to use Surface controls instead of the regular controls.

The Expander's default template can be found at http://msdn.microsoft.com/en-us/library/ms753296.aspx.

All you need to change is the ToggleButton:

<ToggleButton OverridesDefaultStyle="True"
              Template="{StaticResource ExpanderToggleButton}"
              IsChecked="{Binding IsExpanded, Mode=TwoWay, 
              RelativeSource={RelativeSource TemplatedParent}}">

changes to

<s:SurfaceToggleButton OverridesDefaultStyle="True"
                       Template="{StaticResource ExpanderToggleButton}"
                       IsChecked="{Binding IsExpanded, Mode=TwoWay, 
                       RelativeSource={RelativeSource TemplatedParent}}">

(closing tags omitted)

This assumes s is bound to the Surface XML Namespace:

xmlns:s="http://schemas.microsoft.com/surface/2008"



回答2:


I hate to necro such an old topic but this was the top google result and I found a solution that worked better for me.

After my GUI loads I use the VisualTreeHelper recursively to generate a List<DependencyObject> of all the GUI objects at runtime. My project required this GUI object list for other reasons, but it also gave me a very simple solution to adding Touch-support for non-Surface controls.

viewObjectList.ForEach(x =>
        {
            var temp = x as System.Windows.Controls.Primitives.ToggleButton;
            if (temp != null)
            {
                temp.IsManipulationEnabled = true;
                temp.TouchUp += TouchUpEvent;
            }
        });

Iterate through the list of GUI objects, find the ToggleButtons (the actual clickable part of the Expander), turn on their touch-support, and bind an event to them when TouchUp is fired (when a user lifts their finger).

private void TouchUpEvent(object sender, TouchEventArgs e)
    {
        if (TouchesOver.Count() == 1)
        {
            var temp = sender as System.Windows.Controls.Primitives.ToggleButton;
            temp.IsChecked = !temp.IsChecked;
        }
        e.Handled = false;
    }


来源:https://stackoverflow.com/questions/3843257/using-wpf-expander-control-on-microsoft-surface-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!