繁体   English   中英

WPF绑定不适用于自定义UserControl

[英]WPF binding not working on custom UserControl

我已经为此苦苦挣扎了三天,我觉得我已经很接近解决方案了,但是我实在无法实现。

我正在做一个数独难题,我想创建一个自定义控件以显示9个3x3网格之一,因此我将显示9个3x3网格并具有一个不错的9x9网格。

我发现至少有30个不同的页面应该解释如何创建此页面,但是我在每个页面上都找不到解决方案。

我认为问题在于PartialSudokuGrid因为Values属性似乎没有被调用。 另外,在输出窗口中不会显示任何错误。 谁能告诉我我在做什么错?

这并不意味着要转储代码并期望有人对其进行修复,但是我确实坚持了这一点,我认为这只是一点点改变就能使一切正常。

这是我的代码:

主窗口:

<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"
    Title="MainWindow" Height="400" Width="400"
    DataContext="{Binding PartialSudokuGrid, Source={StaticResource Locator}}">

    <UniformGrid Columns="3" Rows="3">
        <local:PartialSudokuGrid Values="{Binding ValuesVM}" />
    </UniformGrid>
</Window>

视图模型:

public class PartialSudokuGridVM : ViewModelBase {

    private int[] _values;

    public PartialSudokuGridVM() {
        this.ValuesVM = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    }

    public int[] ValuesVM {
        get {
            return this._values;
        }
        set {
            this._values = value;
            this.RaisePropertyChanged();
        }
    }
}

用户控件:

<UserControl x:Class="SudokuWPF.PartialSudokuGrid"
             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"
             DataContext="{Binding RelativeSource={RelativeSource Self}, Path=Values}">

    <UniformGrid>
        <TextBox Text="{Binding [0]}" />
        <TextBox Text="{Binding [1]}" />
        <TextBox Text="{Binding [2]}" />
        <TextBox Text="{Binding [3]}" />
        <TextBox Text="{Binding [4]}" />
        <TextBox Text="{Binding [5]}" />
        <TextBox Text="{Binding [6]}" />
        <TextBox Text="{Binding [7]}" />
        <TextBox Text="{Binding [8]}" />
    </UniformGrid>
</UserControl>

后面的代码:

public partial class PartialSudokuGrid : UserControl {

        public PartialSudokuGrid() {
            InitializeComponent();
        }

        public int[] Values {
            get {
                return (int[])GetValue(ValuesProperty);
            }
            set {
                SetValue(ValuesProperty, value);
            }
        }

        public static DependencyProperty ValuesProperty = DependencyProperty.Register("Values", typeof(int[]), typeof(PartialSudokuGrid));
    }

固定:

就像MDoobie所建议的那样,我从PartialGridView中删除了Self绑定,并清除了代码隐藏文件(不再使用)。

旧:

<local:PartialSudokuGrid Values="{Binding ValuesVM}" />

新:

<local:PartialSudokuGrid DataContext="{Binding ValuesVM}" />

我认为您可以通过以下代码行设置Window的DataContext: DataContext="{Binding PartialSudokuGrid, Source={StaticResource Locator}}"

设置为PartialSudokuGrid而不是PartialSudokuGridVM(具有ValuesVM属性)。 尝试将PartialSudokuGridVm设置为DataContext。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM