Drag-and-Drop UWP vs Button Style

2019-07-18 10:24发布

问题:

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.