WPF 中的棋盘

发布于 2024-08-15 16:06:39 字数 186 浏览 9 评论 0原文

多年来我一直使用 Winforms 进行开发,现在我想切换到 WPF 并制作一个棋盘。不幸的是我不知道从哪里开始。使用 WPF 让我非常不确定,我感觉自己又像个菜鸟了。有人可以概述一下基本设计吗?我想我会从 8x8 网格开始,使用矩形作为正方形,使用图像作为碎片。进而?我错过了什么吗?

编辑:这只是关于用户界面;幕后发生的事情是没有问题的。

For years I've developed with Winforms, now I want to switch to WPF and make a chessboard. Unfortunately I have no idea where to start. Using WPF makes me very unsure, I'm feeling like a noob again. Can someone outline a basic design? I guess I would start with a 8x8 Grid and use rectangles for the squares, images for pieces. And then? Am I missing something?

Edit: It's just about the user interface; what goes on behind the scenes is no problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

奢华的一滴泪 2024-08-22 16:06:39

标准网格的替代方案是使用 UniformGrid (msdn 链接)。

它可能更适合这个(在我看来),因为它总是给你相同大小的单元格。

使用ala:

<UniformgGrid Columns="8" Rows="8">
    <Control1/>
    <Control2/>
    <Control3/>
</UniformGrid>

这些答案中的任何一个都会给你你想要的结果。

An alternative to the standard grid, is the use of a UniformGrid (msdn link).

It's probably more suited to this (in my opinion) as it will ALWAYS give you the same size cells.

used ala:

<UniformgGrid Columns="8" Rows="8">
    <Control1/>
    <Control2/>
    <Control3/>
</UniformGrid>

any of these answers will give you the results you desire.

通知家属抬走 2024-08-22 16:06:39

Chess 似乎非常适合 WPF 的 MVVM 代码模式。

模型将成为国际象棋游戏的逻辑,听起来你可以控制它。
View 将呈现游戏的 WPF 外观,ViewModel 将表示游戏,视图可以在其中进行数据绑定。

对于视图,使用 UniformGrid 的 ItemsControl 将适用于游戏的 2D 表示。

这是一个开始(未选中)

<ItemsControl ItemsSource="{Binding TheGame}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="8" Rows="8" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl Background="{Binding SquareColor}">
                <Path Data="{Binding PieceShape}" Fill="{Binding PieceColor}" />
            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

为了使上述工作正常,您的 ViewModel 将需要有一个 ObservableCollection 并且 ChessGridItem 应该是一个 DependencyObject公开 SquareColorPieceColorPieceShape 的 DependencyProperties

Chess seems like a good fit for WPF's MVVM code pattern.

The Model will be the logic for the game of chess, sounds like you have that under control.
The View will the the WPF look of the game, and the ViewModel will the representation of the game, in which the View can databind to.

For the view, an ItemsControl using a UniformGrid will work for a 2D representation of the game.

Here's a start (unchecked)

<ItemsControl ItemsSource="{Binding TheGame}">
    <ItemsControl.ItemsPanel>
        <ItemsPanelTemplate>
            <UniformGrid Columns="8" Rows="8" />
        </ItemsPanelTemplate>
    </ItemsControl.ItemsPanel>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ContentControl Background="{Binding SquareColor}">
                <Path Data="{Binding PieceShape}" Fill="{Binding PieceColor}" />
            </ContentControl>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

For the above to work, your ViewModel will need to have a ObservableCollection<ChessGridItem> and ChessGridItem should be a DependencyObject exposing DependencyProperties for SquareColor, PieceColor and PieceShape

贪恋 2024-08-22 16:06:39

您可以使用 XAML 或代码来实现 UI,并获得相同的结果。我最近开始使用 WPF,我推荐 XAML 方法。一开始有点吓人,但很快就会熟悉。对我来说,这感觉像是一种精心设计的 UI 设计方法,而现在 WinForms 看起来就像是把 .NET 压垮了之前的一切。

您可以从拖放方法开始,但如果您像我一样,您将很快使用 XAML 工作并使用设计界面进行视觉检查。

这可能不是我会做的,但如果您查看过任何 XML 或 HTML,您可能会猜到这将显示什么,即使您以前从未查看过任何 XAML:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>

    <Border Grid.Column="0" Grid.Row="0" Background="Black" />
    <Border Grid.Column="2" Grid.Row="0" Background="Black" />
    <Border Grid.Column="1" Grid.Row="1" Background="Black" />
    <Border Grid.Column="3" Grid.Row="1" Background="Black" />
</Grid>

You could do your UI in XAML or in code with the same results. I've recently started using WPF and I recommend the XAML approach. It's a bit intimidating to start with but it rapidly becomes familiar. To me, it feels like a well-conceived approach to UI design and now WinForms looks like they just slapped .NET over whatever came before.

You can start with the drag and drop approach but if you're like me you'll be working in XAML quite quickly and using the design surface for a visual check.

This is probably not how I would do it but if you've looked at any XML or HTML you can probably guess what this will show, even if you've never looked at any XAML before:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
        <ColumnDefinition Width="100" />
    </Grid.ColumnDefinitions>

    <Grid.RowDefinitions>
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>

    <Border Grid.Column="0" Grid.Row="0" Background="Black" />
    <Border Grid.Column="2" Grid.Row="0" Background="Black" />
    <Border Grid.Column="1" Grid.Row="1" Background="Black" />
    <Border Grid.Column="3" Grid.Row="1" Background="Black" />
</Grid>
画中仙 2024-08-22 16:06:39

不,我认为你根本没有错过任何东西。这是一个好的开始。

创建一个网格,然后添加列和行。然后将一个矩形放入交替的单元格(或图像)中。我将为矩形的填充颜色和描边创建一个样式。这允许您在一个位置(样式定义)更改背景颜色,而不是为需要更改的每个单元格更改背景颜色。

这是一个使用网格的非常基本的板。请注意,我没有对行和列的大小进行硬编码。这将使一切保持比例并允许董事会扩展。

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Name="A1"  />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Rectangle Name="rectangle1"
               Stroke="Black" Fill="Aquamarine" />
</Grid>

No, I don't think you're missing anything at all. That is a good start.

Create a grid then add the columns and rows. Then drop a rectangle into alternating cells (or an image). I would create a style for the rectangle's fill color and stroke. This allows you to change the background color in one location (the style definition) rather than for each cell you need to change.

Here is a very basic board using grid. Note I did not hardcode the size of the rows and columns. Tis will keep everything proportionate and allow the board to scale.

    <Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Name="A1"  />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Rectangle Name="rectangle1"
               Stroke="Black" Fill="Aquamarine" />
</Grid>
太傻旳人生 2024-08-22 16:06:39

我同意这里发布的所有示例,但我认为您有点困惑,因为 WPF 中“数据”的“模板”模型与 WinForms 的纯紧密绑定 UI + 数据模型完全不同。

从 WinForm 到 WPF 的学习曲线并不大,我自己花了两年时间才开始使用 WPF 进行编码,因为我总是更喜欢代码隐藏文件。

我最好的猜测是,首先您必须研究 WPF 的“逻辑树”和“视觉树”概念,在这里您将了解 WPF UI 元素和非 UI 元素(数据对​​象)如何轻松地在 XAML 中连接,而无需编写任何 C#。

另一个最重要的概念是“触发器”。

您需要创建的只是“数据”对象,这些对象将是您的国际象棋项目(King、Horse)等,派生自一个实现 IPropertyChanged 接口的公共基类,并且它们将实现“CanBeKilled”、“IsPossibleTarget”等可以简单绑定的属性在列表框的 ItemTemplate 中,列表框将保存当前选择。

ListBox 的项目面板模板可以是上述示例之一,并且在 MouseOver 上,您可以根据上述属性突出显示颜色或边框。您所要做的就是在选择更改时更新 ListBox 中每个项目的布尔属性。

我刚刚阅读了您的编辑部分,我相信 WPF 中的隐藏代码一定是不同且简单的,因为与 WinForms 相比,设计良好的 WPF 的隐藏代码将比 WinForm 少 90%。

I agree with all samples posted here but I think you are little confused because of completely different "Template" model of "Data" in WPF against pure closely bound UI + Data model of WinForms.

Coming from WinForm to WPF is little big learning curve, I myself took two years to start coding in WPF because I was always more comfertable in codebehind files.

My best guess is first you have to look into "Logical Tree" and "Visual Tree" concepts of WPF, where you will understand that how easlily WPF UI Elements and non UI Elements (Data objects) connects just in XAML without writing any C#.

And another most important concepts like "Triggers".

All you need to create is "Data" objects which will be your chess items (King,Horse) etc, derived from one common base class implementing IPropertyChanged interface and they will implement properties like "CanBeKilled", "IsPossibleTarget" which can be simply bounded in ItemTemplate of ListBox where your ListBox will hold current selection.

ListBox's item panel template can be one of the above examples and on MouseOver, you can highlight color or border according to above properties. And all you have to do is, update Every item's boolean properties in ListBox when the selection changes.

I just read your Edit part and I believe the Code Behind must be different and simple in WPF because comparing with WinForms, nicely designed WPF will have 90% less code behind compared to WinForm.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文