【问题标题】:WPF binding not working on custom UserControlWPF 绑定在自定义 UserControl 上不起作用
【发布时间】:2014-12-13 15:00:59
【问题描述】:

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

我正在制作一个数独游戏,我想创建一个自定义控件来显示九个 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}" />

【问题讨论】:

    标签: c# wpf binding user-controls


    【解决方案1】:

    我想你用这一行 DataContext="{Binding PartialSudokuGrid, Source={StaticResource Locator}}" 设置了 Window 的 DataContext

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

    【讨论】:

    • 这行得通,谢谢!我认为绑定填充了 PartialSudokuGrid 中的属性,但没有。惊人的!我将编辑我的答案以显示修复方法。
    • 很高兴能帮到你。
    猜你喜欢
    • 2018-12-07
    • 2013-04-06
    • 2013-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多