拖放式UWP和按钮样式 [英] Drag-and-Drop UWP vs Button Style
问题描述
另一种情况:我已经点击了关联的事件,当我应用这种风格时崩溃。我不明白问题是什么,有人可以帮助我吗?
谢谢:
代码MainPage:
< ListView x:Name =MyListViewItemsSource ={x:Bind _ObservableCollection} Style ={StaticResource MyListViewStyle}SelectionMode =NoneCanDragItems =TrueDragItemsStarting =MyListView_OnDragItemsStarting>
< ListView.ItemTemplate>
< DataTemplate>
< Button Tapped =Item_TappedStyle ={StaticResource MyButtonStyle}/>
< / DataTemplate>
< /ListView.ItemTemplate>
< / ListView>
XAML样式代码
< Style x:Key =MyButtonStyleTargetType =Button>
< Setter Property =BackgroundValue ={StaticResource MyColor1}/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType =Button>
< Border x:Name =ButtonContentBackground ={TemplateBinding Background}>
< VisualStateManager.VisualStateGroups>
< VisualStateGroup x:Name =CommonStates>
< VisualState x:Name =Normal/>
< VisualState x:Name =PointerOver>
< Storyboard>
< ColorAnimation Storyboard.TargetName =ButtonContentTo ={StaticResource MyColor2}
Storyboard.TargetProperty =(Border.Background)(SolidColorBrush.Color)
持续时间= 00:00:00.1\" />
< / Storyboard>
< / VisualState>
< VisualState x:Name =Pressed>
< Storyboard>
< PointerDownThemeAnimation TargetName =ButtonContent/>
< ColorAnimation Storyboard.TargetName =ButtonContentTo ={StaticResource MyColor3}
Storyboard.TargetProperty =(Border.Background)。(SolidColorBrush.Color)
持续时间= 00:00:00.1\" />
< / Storyboard>
< / VisualState>
< / VisualStateGroup>
< /VisualStateManager.VisualStateGroups>
< Image Width =200Height =200>
< Image.Source>
< BitmapImage UriSource ={Binding MyImage,Mode = OneTime}/>
< /Image.Source>
< / Image>
< / Border>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< / Style>
您最好的解决方案是不要使用按钮
。在 ListView
中使用 IsItemClickEnabled
属性和 ItemClick
事件,然后将您的图像放在 ItemContainerStyle
中。它将修复您的拖放问题,关注问题并导致更好的性能。
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>
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.
这篇关于拖放式UWP和按钮样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!