Drag-and-Drop UWP vs Button Style

只谈情不闲聊 提交于 2020-01-15 03:30:06

问题


I have a question that is me to leave upset. I want to drag and drop an item in a listview and I can not do when I have a style applied to my item. Only I can do and is working perfectly when I have no style (MyButtonStyle) applied / or do not have this image in style. When I have style (MyButtonStyle), ItemDragStarting event is not called.

Another situation: I have tapped associated event, and when I apply this style crashes. I do not understand what the problem is, can someone help me?

Thank you:

Code MainPage:

<ListView x:Name="MyListView" ItemsSource="{x:Bind _ObservableCollection}" Style="{StaticResource MyListViewStyle}" SelectionMode="None" CanDragItems="True" DragItemsStarting="MyListView_OnDragItemsStarting">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Button Tapped="Item_Tapped" Style="{StaticResource MyButtonStyle}" />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

XAML Style code:

 <Style x:Key="MyButtonStyle" TargetType="Button">
  <Setter Property="Background" Value="{StaticResource MyColor1}"/>
    <Setter Property="Template">
          <Setter.Value>
            <ControlTemplate TargetType="Button">
                <Border x:Name="ButtonContent" Background="{TemplateBinding Background}">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="PointerOver">
                                <Storyboard>
                                    <ColorAnimation Storyboard.TargetName="ButtonContent" To="{StaticResource MyColor2}" 
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" 
                                        Duration="00:00:00.1"/>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Pressed">
                                <Storyboard>
                                    <PointerDownThemeAnimation TargetName="ButtonContent"/>
                                    <ColorAnimation Storyboard.TargetName="ButtonContent" To="{StaticResource MyColor3}" 
                                        Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" 
                                        Duration="00:00:00.1"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Image Width="200" Height="200">
                        <Image.Source>
                            <BitmapImage UriSource="{Binding MyImage, Mode=OneTime}" />
                        </Image.Source>
                    </Image>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

回答1:


The best solution for you would be to not use a Button at all. Use IsItemClickEnabled property and ItemClick event on the ListView instead, then put your image in the ItemContainerStyle. It will fix your drag and drop issues, focus problems and result in better performance.



来源:https://stackoverflow.com/questions/35794887/drag-and-drop-uwp-vs-button-style

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