Вот мой MainWindow.xaml:
<Window x:Class="View.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:controls="clr-namespace:View.Controls"
xmlns:local="clr-namespace:View"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<controls:PiCrossControl x:Name="picrossControl">
<controls:PiCrossControl.SquareTemplate>
<DataTemplate>
<Rectangle Width="32" Height="32" Stroke="Black">
<Rectangle.Fill>
<Binding Path="Contents.Value">
<Binding.Converter>
<local:SquareConverter Empty="White" Filled="Black" Unknwon="Gray"/>
</Binding.Converter>
</Binding>
</Rectangle.Fill>
</Rectangle>
</DataTemplate>
</controls:PiCrossControl.SquareTemplate>
<controls:PiCrossControl.RowConstraintsTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Values}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Width="32" Height="32" Text="{Binding Value}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</controls:PiCrossControl.RowConstraintsTemplate>
<controls:PiCrossControl.ColumnConstraintsTemplate>
<DataTemplate>
<ItemsControl ItemsSource="{Binding Values}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Orientation="Vertical"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Width="32" Height="32" Text="{Binding Value}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
</controls:PiCrossControl.ColumnConstraintsTemplate>
</controls:PiCrossControl>
</Grid>
</Window>
А вот мой MainWindow.xaml.cs:
using DataStructures;
using PiCross;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Grid = DataStructures.Grid;
using Size = DataStructures.Size;
namespace View
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var puzzle = Puzzle.FromRowStrings(
"xxxxx",
"x...x",
"x...x",
"x...x",
"xxxxx"
);
var facade = new PiCrossFacade();
var playablePuzzle = facade.CreatePlayablePuzzle(puzzle);
picrossControl.Grid = playablePuzzle.Grid;
picrossControl.RowConstraints = playablePuzzle.RowConstraints;
picrossControl.ColumnConstraints = playablePuzzle.ColumnConstraints;
picrossControl.MouseLeftButtonDown += PicrossControl_MouseLeftButtonDown;
}
private void PicrossControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show(this, "Youclikced the stuff", sender.ToString());
}
}
public class SquareConverter : IValueConverter
{
public object Filled { get; set; }
public object Empty { get; set; }
public object Unknwon { get; set; }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var square = (Square)value;
if (square == Square.EMPTY)
{
return Empty;
}
if (square == Square.FILLED)
{
return Filled;
}
else
{
return Unknwon;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}
I уже был в состоянии добавить триггерное событие MouseLeftButtonDown в picrossControl. Но когда я запускаю это, вы получаете следующее:
Я хотел бы сделать так, чтобы при щелчке по прямоугольникам они меняли цвет. Чтобы сделать это, я ожидаю, что мне нужно добавить триггерное событие MouseLeftButtonDown к прямоугольникам, однако я не знаю, как это сделать. Потому что я не понимаю, как я могу получить к ним доступ в моем файле .xaml.cs . Вы можете мне помочь?