0

Есть 64 клетки и нужно добавить перемещение коня(его изображения) по полю. Я передавал позицию коня через CommandParameter, так и сделал его перемещение, но не могу теперь привязать картинку.

Xaml код:

<Window x:Class="Cursovaya.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:Cursovaya.ViewModels"
    mc:Ignorable="d"
    Title="MainWindow" Height="650" Width="600">
<Window.Resources>
    <Style x:Key="LightCell" TargetType="Button">
        <Setter Property="BorderThickness" Value="0"/>
        <Setter Property="Command" Value="{Binding KnightMove}"/>
        <Setter Property="Background" Value="Bisque"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Content" Value="123"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" 
                            BorderThickness="{TemplateBinding BorderThickness}"/>
                &lt;ControlTemplate.Triggers&gt;
                    &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
                        &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;1&quot;/&gt;
                    &lt;/Trigger&gt;

                &lt;/ControlTemplate.Triggers&gt;

            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;

&lt;Style x:Key=&quot;DarkCell&quot; TargetType=&quot;Button&quot;&gt;
    &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;0&quot;/&gt;
    &lt;Setter Property=&quot;Command&quot; Value=&quot;{Binding KnightMove}&quot;/&gt;
    &lt;Setter Property=&quot;Background&quot; Value=&quot;SandyBrown&quot;/&gt;
    &lt;Setter Property=&quot;BorderBrush&quot; Value=&quot;Black&quot;/&gt;
    &lt;Setter Property=&quot;Template&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;ControlTemplate TargetType=&quot;Button&quot;&gt;
                &lt;Border Background=&quot;{TemplateBinding Background}&quot; BorderBrush=&quot;{TemplateBinding BorderBrush}&quot;
                        BorderThickness=&quot;{TemplateBinding BorderThickness}&quot;/&gt;
                &lt;ControlTemplate.Triggers&gt;
                    &lt;Trigger Property=&quot;IsMouseOver&quot; Value=&quot;True&quot;&gt;
                        &lt;Setter Property=&quot;BorderThickness&quot; Value=&quot;1&quot;/&gt;
                    &lt;/Trigger&gt;
                &lt;/ControlTemplate.Triggers&gt;
            &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;

</Window.Resources>

<Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="10"/> </Grid.RowDefinitions> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition Width="8"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="8*"/> <RowDefinition/> </Grid.RowDefinitions>

    &lt;Grid Grid.Row=&quot;1&quot; Grid.Column=&quot;1&quot;&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
            &lt;ColumnDefinition/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
            &lt;RowDefinition/&gt;
        &lt;/Grid.RowDefinitions&gt;


        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;0&quot; Content=&quot;123&quot; CommandParameter=&quot;1&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;0&quot; Content=&quot;123&quot; CommandParameter=&quot;2&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;0&quot; CommandParameter=&quot;3&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;0&quot; CommandParameter=&quot;4&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;0&quot; CommandParameter=&quot;5&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;0&quot; CommandParameter=&quot;6&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;0&quot; CommandParameter=&quot;7&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;0&quot; CommandParameter=&quot;8&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;9&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;10&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;11&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;12&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;13&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;14&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;15&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;1&quot; CommandParameter=&quot;16&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;17&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;18&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;19&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;20&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;21&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;22&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;23&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;2&quot; CommandParameter=&quot;24&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;25&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;26&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;27&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;28&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;29&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;30&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;31&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;3&quot; CommandParameter=&quot;32&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;33&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;34&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;35&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;36&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;37&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;38&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;39&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;4&quot; CommandParameter=&quot;40&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;41&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;42&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;43&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;44&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;45&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;46&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;47&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;5&quot; CommandParameter=&quot;48&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;49&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;50&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;51&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;52&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;53&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;54&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;55&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;6&quot; CommandParameter=&quot;56&quot;/&gt;

        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;57&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;1&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;58&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;2&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;59&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;3&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;60&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;4&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;61&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;5&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;62&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource DarkCell}&quot; Grid.Column=&quot;6&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;63&quot;/&gt;
        &lt;Button Style=&quot;{StaticResource LightCell}&quot; Grid.Column=&quot;7&quot; Grid.Row=&quot;7&quot; CommandParameter=&quot;64&quot;/&gt;
    &lt;/Grid&gt;
&lt;/Grid&gt;

</Grid>

ViewModel

internal class ApplicationViewModel : INotifyPropertyChanged
{
    public ObservableCollection<Board> MyBoard { get; set; }
private int xKnightPosition = 0; 
private int yKnightPosition = 0;
private int[] lastSelectedCell = new int[2]; // первое значение х второе у
private int[,] myDesk = new int[8,8];
private bool moveLocker = true;
public ApplicationViewModel()
{
    MyBoard = new ObservableCollection&lt;Board&gt;();
    Board board = new Board { XPosition = 4, YPosition = 4, Desk = myDesk }; // задаём начальные значения
    MyBoard.Insert(0, board);
    CreateDesk();
}
public void CreateDesk()
{
    for(int i = 0; i &lt; 8; i++)
    {
        for(int j = 0; j &lt; 8; j++)
        {
            myDesk[i, j] = 0;
        }
    }
    myDesk[MyBoard[0].XPosition, MyBoard[0].YPosition] = 1;
}



void SelectedCell(object obj)
{
    int position = Convert.ToInt32(obj);
    xKnightPosition = 0;
    yKnightPosition = 0;
    bool flag = true;
    while (flag == true)
    {
        if (position &lt;= 8)
        {
            xKnightPosition = position - 1;
            flag = false;
        }
        else
        {
            yKnightPosition++;
            position -= 8;
        }
    }
}
public bool IsKnightSelected()
{
    if (MyBoard[0].Desk[xKnightPosition, yKnightPosition] == 1)
        return true;
    else
        return false;
}
public bool CanMove()
{
    if (xKnightPosition == lastSelectedCell[0] - 1 &amp;&amp; yKnightPosition == lastSelectedCell[1] - 2)   // -1 -2
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] - 2 &amp;&amp; yKnightPosition == lastSelectedCell[1] - 1)   // -2 -1
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] - 2 &amp;&amp; yKnightPosition == lastSelectedCell[1] + 1)   // -2 +1
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] - 1 &amp;&amp; yKnightPosition == lastSelectedCell[1] + 2)   // -1 +2
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] + 1 &amp;&amp; yKnightPosition == lastSelectedCell[1] + 2)   // +1 +2
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] + 2 &amp;&amp; yKnightPosition == lastSelectedCell[1] + 1)   // +2 +1
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] + 2 &amp;&amp; yKnightPosition == lastSelectedCell[1] - 1)   // +2 -1
    {
        return true;
    }
    else if (xKnightPosition == lastSelectedCell[0] + 1 &amp;&amp; yKnightPosition == lastSelectedCell[1] - 2)   // +1 -2
    {
        return true;
    }
    else
        return false;
}


private RelayCommand knightMove;
public RelayCommand KnightMove
{
    set { knightMove = value; }
    get { return knightMove ??
            (knightMove = new RelayCommand(obj =&gt;
            {

                SelectedCell(obj);

                if (moveLocker == false)
                {
                    if(CanMove() == true)
                    {
                        Board board = new Board { Desk = myDesk, XPosition = xKnightPosition, YPosition = yKnightPosition };
                        MyBoard.Insert(0, board);
                        CreateDesk();
                    }
                    moveLocker = true;
                }
                else if (IsKnightSelected() == true)
                {
                    moveLocker = false;
                    MessageBox.Show(&quot;Knight is hear!&quot;);
                }

                lastSelectedCell[0] = xKnightPosition;
                lastSelectedCell[1] = yKnightPosition;

            })) ;
        }
}



public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = &quot;&quot;)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(prop));
}

}

  • Шахматы чтоли? Вас не смутило 64 строчки одинакового кода? https://ru.stackoverflow.com/a/1255542/373567 вот решение, скачайте протестирйте, изучите. – aepot Dec 11 '22 at 10:11
  • Не совсем шахматы. Игра "Ход Конём". Посмотрел ваш пример и, если честно, всё немного сложно для меня – Денис Старченко Dec 12 '22 at 11:31
  • Тот же MVVM, ничего сложного. Выкидываете лишние фигуры и готово. Пример можно скачать и запустить, посмотреть как работает. Доска наверное вообще 1 в 1 нужна. – aepot Dec 12 '22 at 11:41
  • 1
    Скачал проект, сейчас разбираюсь. На самом деле в студии выглядит не так страшно) Смотрю и понимаю, что я не в том направлении двигался. Спасибо большое! – Денис Старченко Dec 12 '22 at 12:31

0 Answers0