多次使用同一词典模板

本文关键字: | 更新日期: 2023-09-27 18:27:26

我正在尝试使用9x9网格制作数独,但为了拥有干净的代码,我正在尝试字典。在编写了下面给出的代码后,预览实现了9x9网格。然而,每当我启动应用程序时,我都不会得到相同的结果,我只得到最后一个加载的网格。

这是一个具有以下网格元素的字典:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x:Key="GridTemplate" ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label Grid.Column="0" Grid.Row="0" ></Label>
    <Label Grid.Column="0" Grid.Row="1" ></Label>
    <Label Grid.Column="0" Grid.Row="2" ></Label>
    <Label Grid.Column="1" Grid.Row="0" ></Label>
    <Label Grid.Column="1" Grid.Row="1" ></Label>
    <Label Grid.Column="1" Grid.Row="2" ></Label>
    <Label Grid.Column="2" Grid.Row="0" ></Label>
    <Label Grid.Column="2" Grid.Row="1" ></Label>
    <Label Grid.Column="2" Grid.Row="2" ></Label>
</Grid>

现在我正试图在这个窗口的另一个网格中使用这个9次

<Window x:Class="SudokuWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="GridDictonary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="0" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="1" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="2" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="1" Grid.Row="0" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="1" Grid.Row="1" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="1" Grid.Row="2" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="2" Grid.Row="0" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="2" Grid.Row="1" />
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="2" Grid.Row="2" />
</Grid>

我希望这个问题能得到解决,因为我没有选择:(

多次使用同一词典模板

将资源包装在用户控件中可能会有所帮助?

这在我的机器上起作用(无需填充数字)

页码:

<Page x:Class="WPFAnswers.Ans34384501.P1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:local="clr-namespace:WPFAnswers.Ans34384501"
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
  Title="P1">
<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <local:UC1 Grid.Column="0" Grid.Row="0"></local:UC1>
    <local:UC1 Grid.Column="0" Grid.Row="1"></local:UC1>
    <local:UC1 Grid.Column="0" Grid.Row="2"></local:UC1>
    <local:UC1 Grid.Column="1" Grid.Row="0"></local:UC1>
    <local:UC1 Grid.Column="1" Grid.Row="1"></local:UC1>
    <local:UC1 Grid.Column="1" Grid.Row="2"></local:UC1>
    <local:UC1 Grid.Column="2" Grid.Row="0"></local:UC1>
    <local:UC1 Grid.Column="2" Grid.Row="1"></local:UC1>
    <local:UC1 Grid.Column="2" Grid.Row="2"></local:UC1>
</Grid>

用户控制:

<UserControl x:Class="WPFAnswers.Ans34384501.UC1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:WPFAnswers.Ans34384501"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Control.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="GridDictonary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Control.Resources>
<Grid >
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
    </Grid.RowDefinitions>
    <ContentControl Content="{StaticResource GridTemplate}" Grid.Column="0" Grid.Row="0" />
</Grid>

资源:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:WPFAnswers.Ans34384501">
<Grid x:Key="GridTemplate" ShowGridLines="True">
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition />
        <RowDefinition />
    </Grid.RowDefinitions>
    <Label Grid.Column="0" Grid.Row="0" ></Label>
    <Label Grid.Column="0" Grid.Row="1" ></Label>
    <Label Grid.Column="0" Grid.Row="2" ></Label>
    <Label Grid.Column="1" Grid.Row="0" ></Label>
    <Label Grid.Column="1" Grid.Row="1" ></Label>
    <Label Grid.Column="1" Grid.Row="2" ></Label>
    <Label Grid.Column="2" Grid.Row="0" ></Label>
    <Label Grid.Column="2" Grid.Row="1" ></Label>
    <Label Grid.Column="2" Grid.Row="2" ></Label>
</Grid>

我真的不确定,但我认为发生这种情况是因为你不能将同一个Instance添加为2个或更多元素的子元素,在这种情况下,每个ContentControl都将你的网格添加为子元素,这是无效的,你应该改为尝试DataTemplate。

<DataTemplate x:Key="GridTemplate">
<Grid  ShowGridLines="True">
<Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
    <ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
    <RowDefinition />
    <RowDefinition />
    <RowDefinition />
</Grid.RowDefinitions>
<Label Grid.Column="0" Grid.Row="0" ></Label>
<Label Grid.Column="0" Grid.Row="1" ></Label>
<Label Grid.Column="0" Grid.Row="2" ></Label>
<Label Grid.Column="1" Grid.Row="0" ></Label>
<Label Grid.Column="1" Grid.Row="1" ></Label>
<Label Grid.Column="1" Grid.Row="2" ></Label>
<Label Grid.Column="2" Grid.Row="0" ></Label>
<Label Grid.Column="2" Grid.Row="1" ></Label>
<Label Grid.Column="2" Grid.Row="2" ></Label>
</Grid>
</DataTemplate>
相关文章:
  • 没有找到相关文章