0

插入表格时,我试图获得与 MS Word 中相同的行为。当我在矩形网格上移动时,我希望矩形变成蓝色,显示表格应该有多少列和行。

我改编了我找到的一个示例,但是当我希望当前矩形左侧和上方的所有矩形都更改填充时,我遇到了困难。

在我的 ViewModel 中,我创建了一个包含 8 个项目的集合:

ObservableCollection<BoardTileViewModel> board = new ObservableCollection<BoardTileViewModel>();
for (int i = 0; i < 8; i++)
{
    board.Add(new BoardTileViewModel(this, i % 4, i / 4));
}
this.Board = new ReadOnlyObservableCollection<BoardTileViewModel>(board);

如您所见,BoardTileViewModel 通过构造函数获取 X/Y 坐标,因此可以用它来做一些事情。我对 ItemsControl 使用以下样式:

<ResourceDictionary
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <Style x:Key="TileButtonStyle" TargetType="Button">
        <Setter Property="Margin" Value="0 0 1 1" />
        <Setter Property="Width" Value="30" />
        <Setter Property="Height" Value="30" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid>
                        <Rectangle x:Name="PART_Rectangle" Fill="White" Stroke="Black"></Rectangle>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="PART_Rectangle" Property="Fill" Value="RoyalBlue" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="BoardItemsControlStyle" TargetType="ItemsControl">
        <Setter Property="Width" Value="124" />
        <Setter Property="Height" Value="62" />
        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <Button Style="{StaticResource TileButtonStyle}" Command="{Binding Check}" />
                </DataTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <WrapPanel />
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

有没有人建议如何设置鼠标所在矩形左侧和上方的所有矩形的填充?

4

1 回答 1

0

我成功了!

如果有人想知道解决方案,这就是我所做的:

  1. I have moved the styles from the resourcedictionary to the Window (to be able to add a MouseEnter-event).
  2. I implemented an INotifyPropertyChanged in the BoardTileViewModel and added a Fill property. I bound the Fill property to the Fill of the Rectangle.
  3. In the MouseEnter-event I get for the other rectangles and set their Fill property.

This made the Trigger useless, so I removed it from the ControlTemplate.

于 2013-04-12T09:07:38.510 回答