如何更改字典

时间:2015-12-21 20:50:49

标签: c# wpf dictionary

我有这个Dictonary,附带了一个userControl。我成功地将其添加到了我的窗口(感谢我之前关于堆栈溢出的问题 - Link - )在能够向反手添加数据后,我想将其更新回UI。然而,我更喜欢如何实现这一点,因为我的maingrid现在包含了在给定的dictonary中声明的9,3x3网格。

这是dictonary:

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

链接到此用户控件

<UserControl x:Class="SudokuWPF.UserControlSudoku"
         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" 
         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>

最后给出了窗口

<Window x:Class="SudokuWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SudokuWPF"
    DataContext="{Binding Main, Source={StaticResource Locator}}"
    Title="SudokuWindow" Height="350" Width="525">

    <Grid Height="280" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>

        <local:UserControlSudoku Grid.Column="0" Grid.Row="0" />
        <local:UserControlSudoku Grid.Column="0" Grid.Row="1" />
        <local:UserControlSudoku Grid.Column="0" Grid.Row="2" />
        <local:UserControlSudoku Grid.Column="1" Grid.Row="0" />
        <local:UserControlSudoku Grid.Column="1" Grid.Row="1" />
        <local:UserControlSudoku Grid.Column="1" Grid.Row="2" />
        <local:UserControlSudoku Grid.Column="2" Grid.Row="0" />
        <local:UserControlSudoku Grid.Column="2" Grid.Row="1" />
        <local:UserControlSudoku Grid.Column="2" Grid.Row="2" />

        <Rectangle Stroke="Black" Grid.Column="0" Grid.Row="0" />
        <Rectangle Stroke="Black" Grid.Column="0" Grid.Row="1" />
        <Rectangle Stroke="Black" Grid.Column="0" Grid.Row="2" />
        <Rectangle Stroke="Black" Grid.Column="1" Grid.Row="0" />
        <Rectangle Stroke="Black" Grid.Column="1" Grid.Row="1" />
        <Rectangle Stroke="Black" Grid.Column="1" Grid.Row="2" />
        <Rectangle Stroke="Black" Grid.Column="2" Grid.Row="0" />
        <Rectangle Stroke="Black" Grid.Column="2" Grid.Row="1" />
        <Rectangle Stroke="Black" Grid.Column="2" Grid.Row="2" />

    </Grid>

注意:我必然会使用已经拥有数据结构的预制.dll,所以不幸的是我不能在反手中拥有一组数据,所以我正在寻找适用于每个标签的解决方案。

感谢前进^^

0 个答案:

没有答案