多次使用相同的字典模板? [英] Using same dictionary template multiple times?

查看:125
本文介绍了多次使用相同的字典模板?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用一个9x9格式的数字,以便有干净的代码我正在尝试字典。编写下面给出的代码之后,预览就可以实现9x9网格。然而,无论何时启动应用程序,我都不会得到相同的结果,我只得到最后一个加载的网格。

I'm trying to make sudoku using a 9x9 grid however in order to have clean code i'm trying out dictionaries. After writing the code given below the preview achieves the 9x9 grid. However whenever I launch the application I don't get the same result I only get the last loaded grid.

这是具有以下网格元素的字典: p>

this is the dictionary that has the following grid element:

<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次进入另一个网格

Now i'm trying to use this 9 times into another grid into this window

<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>

我希望有这个问题的解决办法是因为我没有选择:(

I"m hoping there is a fix for this problem cause i'm out of options :(

推荐答案

将资源包装到用户中可能是有帮助的

It might be helpful to wrap the resource in a user control instead?

这在我的机器上工作(没有填写数字)

This worked on my machine (sans populating the numbers)

页面:

<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>

这篇关于多次使用相同的字典模板?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆