我对WPF没有经验,所以请原谅我缺乏理解。
编辑:我正在制作一个简单的国际象棋GUI,这些棋子移动得很好,直到一个人试图移动一个已被移动的棋子。如果我们尝试将棋子移动到之前占用的方格,则点击事件根本不会被激活。如果我们尝试将其移动到之前未触及的方块,则会触发该事件,但UI不会更新。
这里是棋盘的XAML(从这里借来:WPF controls needed to build chess application):
select gs.ts,
(select count(*)
from data_store ds
where ds.starttime >= g.ts and ds.starttime < g.ts + interval '4 second'
) as cnt
from (select generate_series(min(starttime), max(strttime), interval '4 second') as ts
from data_store
) g;
我已经省略了所有作品的多数据触发器,因为它们都很相似。
这是视图模型。我正在使用MVVMLight库:
<Window x:Class="Client.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Client"
xmlns:defaults="clr-namespace:butnotquite.Defaults;assembly=butnotquite"
mc:Ignorable="d"
Title="butnotquite Chess" Height="600" Width="600">
<Window.Resources>
<DrawingBrush x:Key="Checkerboard" Stretch="None" TileMode="Tile" Viewport="0,0,2,2" ViewportUnits="Absolute">
<DrawingBrush.Drawing>
<DrawingGroup>
<GeometryDrawing Brush="Tan">
<GeometryDrawing.Geometry>
<RectangleGeometry Rect="0,0,2,2" />
</GeometryDrawing.Geometry>
</GeometryDrawing>
<GeometryDrawing Brush="Brown">
<GeometryDrawing.Geometry>
<GeometryGroup>
<RectangleGeometry Rect="0,0,1,1" />
<RectangleGeometry Rect="1,1,1,1" />
</GeometryGroup>
</GeometryDrawing.Geometry>
</GeometryDrawing>
</DrawingGroup>
</DrawingBrush.Drawing>
</DrawingBrush>
<Style x:Key="PieceStyle" TargetType="{x:Type Image}">
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding PieceType}" Value="{x:Static defaults:PieceType.None}"/>
<Condition Binding="{Binding Color}" Value="{x:Static defaults:Color.None}"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Image.Source" Value="/Images/empty_square.png" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding PieceType}" Value="{x:Static defaults:PieceType.Pawn}"/>
<Condition Binding="{Binding Color}" Value="{x:Static defaults:Color.White}"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Image.Source" Value="/Images/white_pawn.png" />
</MultiDataTrigger.Setters>
</MultiDataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Viewbox>
<ItemsControl Name="ChessboardUI">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width="8" Height="8" Background="{StaticResource Checkerboard}"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Border Name="square" PreviewMouseDown="square_MouseLeftButtonDown" Width="1" Height="1" >
<Image Width="0.8" Height="0.8" Style="{StaticResource PieceStyle}"/>
</Border>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Position.X}" />
<Setter Property="Canvas.Top" Value="{Binding Position.Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</Viewbox>
</Window>
以下是我填写ObservableCollection的方法:
public class PieceViewModel : ViewModelBase
{
private PieceType pieceType;
private Color color;
private Point position;
public PieceType PieceType
{
get
{
return this.pieceType;
}
set
{
this.pieceType = value;
base.RaisePropertyChanged(() => this.PieceType);
}
}
public Color Color
{
get
{
return this.color;
}
set
{
this.color = value;
base.RaisePropertyChanged(() => this.Color);
}
}
public Point Position
{
get
{
return this.position;
}
set
{
this.position = value;
base.RaisePropertyChanged(() => this.Position);
}
}
}
可在此处找到更多代码:https://github.com/YouJinTou/butnotquite/tree/master/Client
感谢您的帮助。
答案 0 :(得分:0)
正如lokusking所评论的那样,集合只需要实例化一次,这就是这里的情况;然而,我没有清除它并在每次点击后重新绑定它。这似乎是一个hackish解决方案,但它的确有效。