Binding UserControl Dependencyproperty未通过绑定更新

Binding UserControl Dependencyproperty未通过绑定更新,binding,user-controls,windows-8,mvvm-light,dependency-properties,Binding,User Controls,Windows 8,Mvvm Light,Dependency Properties,我正在创建一个Windows8应用程序,在过去的几天里,我一直在为自定义用户控件而挣扎。我真的搞不清楚出了什么问题 奇怪的是,当我在代码中更改源代码时,dependencyproperty调用propertychanged事件,但绑定时它不会更新 这是我的代码: GamePage.xaml.cs public sealed partial class GamePage { GamePageViewModel viewModel; public GameP

我正在创建一个Windows8应用程序,在过去的几天里,我一直在为自定义用户控件而挣扎。我真的搞不清楚出了什么问题

奇怪的是,当我在代码中更改源代码时,dependencyproperty调用propertychanged事件,但绑定时它不会更新

这是我的代码:

GamePage.xaml.cs

public sealed partial class GamePage
    {
        GamePageViewModel viewModel;

        public GamePage()
        {
            this.InitializeComponent();
            viewModel = new GamePageViewModel();
        }
    }  
GamePage.xaml

<common:LayoutAwarePage x:Class="WordSearcher.GamePage"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        xmlns:common="using:WordSearcher.Common"
                        xmlns:controls="using:WordSearcher.Controls"
                        xmlns:ignore="http://www.ignore.com"
                        mc:Ignorable="d ignore"
                        d:DesignHeight="768"
                        d:DesignWidth="1366"
                        DataContext="{Binding GamePageViewModel, Source={StaticResource Locator}}">
<StackPanel Orientation="Horizontal">
        <StackPanel.Background>
            <ImageBrush ImageSource="Assets/Wood.jpg" Stretch="UniformToFill"/>
        </StackPanel.Background>
<controls:PuzzleControl Source="{Binding Path=PuzzleData}"/>

    </StackPanel>
</common:LayoutAwarePage>

GamePageViewModel.cs

public class GamePageViewModel : ViewModelBase
    {
        private List<string> _puzzleData;

        public List<string> PuzzleData
        {
            get
            {
                return _puzzleData;
            }
            set
            {
                this._puzzleData = value;
                RaisePropertyChanged("PuzzleData");
            }
        }

        public GamePageViewModel()
        {
            SetNewData();
        }

        private async void SetNewData()
        {
            await SomeManager.Prepare();
            PuzzleData = SomeManager.Create(20);
        }
    }
公共类GamePageViewModel:ViewModelBase
{
私有列表数据;
公共列表数据
{
得到
{
返回数据;
}
设置
{
这个。_数据=值;
RaisePropertyChanged(“拼图数据”);
}
}
公共游戏页面视图模型()
{
SetNewData();
}
私有异步void SetNewData()
{
等待某位经理;
PuzzleData=SomeManager.Create(20);
}
}
puzzcontrol.xaml.cs

<UserControl
    x:Class="WordSearcher.Controls.PuzzleControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WordSearcher.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="500"
    d:DesignWidth="500">

    <Grid x:Name="puzzleGrid" 
          Width="500" 
          Height="500"
          >

    </Grid>
</UserControl>
public sealed partial class PuzzleControl : UserControl
    {

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ObservableCollection<string>), typeof(PuzzleControl), new PropertyMetadata(null, PropertyChanged));

        private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //
            // not called from binding
            //
            ((PuzzleControl)d).OnItemsSourcePropertyChanged(e);
        }

        private void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            Source = (ObservableCollection<string>)e.NewValue;
            SetGridData();
        }

        public ObservableCollection<string> Source
        {
            get
            {
                return (ObservableCollection<string>)GetValue(SourceProperty);
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }

        public PuzzleControl()
        {
            this.InitializeComponent();
            CreateRowsAndColumns();
        }

        private void CreateRowsAndColumns()
        {
            //create rows and columns in puzzleGrid
            //works fine
        }

        private void SetGridData()
        {
            //fill puzzleGrid with data
            //works fine
        }
    }

puzzcontrol.xaml.cs

<UserControl
    x:Class="WordSearcher.Controls.PuzzleControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WordSearcher.Controls"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="500"
    d:DesignWidth="500">

    <Grid x:Name="puzzleGrid" 
          Width="500" 
          Height="500"
          >

    </Grid>
</UserControl>
public sealed partial class PuzzleControl : UserControl
    {

        public static readonly DependencyProperty SourceProperty =
            DependencyProperty.Register("Source", typeof(ObservableCollection<string>), typeof(PuzzleControl), new PropertyMetadata(null, PropertyChanged));

        private static void PropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            //
            // not called from binding
            //
            ((PuzzleControl)d).OnItemsSourcePropertyChanged(e);
        }

        private void OnItemsSourcePropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            Source = (ObservableCollection<string>)e.NewValue;
            SetGridData();
        }

        public ObservableCollection<string> Source
        {
            get
            {
                return (ObservableCollection<string>)GetValue(SourceProperty);
            }
            set
            {
                SetValue(SourceProperty, value);
            }
        }

        public PuzzleControl()
        {
            this.InitializeComponent();
            CreateRowsAndColumns();
        }

        private void CreateRowsAndColumns()
        {
            //create rows and columns in puzzleGrid
            //works fine
        }

        private void SetGridData()
        {
            //fill puzzleGrid with data
            //works fine
        }
    }
公共密封部分类控件:UserControl
{
公共静态只读DependencyProperty SourceProperty=
DependencyProperty.Register(“源”、typeof(ObservableCollection)、typeof(PuzzleControl)、new PropertyMetadata(null、PropertyChanged));
私有静态void属性已更改(DependencyObject d、DependencyPropertyChangedEventArgs e)
{
//
//未从绑定调用
//
((b)控制)d)源属性改变(e);
}
private void OnItemSourceProperty已更改(DependencyPropertyChangedEventArgs e)
{
Source=(ObservableCollection)e.NewValue;
SetGridData();
}
公共可观测收集源
{
得到
{
返回(ObservableCollection)GetValue(SourceProperty);
}
设置
{
SetValue(SourceProperty,value);
}
}
公共交通管制()
{
this.InitializeComponent();
CreateRowsAndColumns();
}
私有void CreateRowsAndColumns()
{
//在网格中创建行和列
//很好
}
私有void SetGridData()
{
//用数据填充网格
//很好
}
}
有人知道我的代码中有错误吗?因为当我把Source=newobservedcollection()放进去时;在PuzzleData的构造函数中,将引发PropertyChanged事件。它与DataContext有关吗

提前通知

我不确定

但是您设置了

puzzdata=List

Source=observetecollection
如果绑定甚至在第一次工作(它显然是这样做的),那么可能是源以某种方式被设置为列出
,而不是
可观察的收集
。这可能就是为什么不调用dependencyproperty方法(
PropertyChanged
)的原因,因为它已注册到
ObservableCollection

但这都是纯粹的猜测,还没有测试过



在我得到他的代码并对其进行审查后,我发现数据从未真正设置过,这就是错误。。。假警报…

您确定要绑定上下文吗?如何绑定对象?如果像在gridview中一样使用用户控件,则DataContext会发生更改,并且根页面的DataContext会发生差异

<controls:PuzzleControl Source="{Binding Path=DataContext.PuzzleData}"/>

如果使用子控件,则为用户控件;按如下方式绑定ElementName属性:

<controls:PuzzleControl Source="{Binding Path=DataContext.PuzzleData, ElementName=pageRoot}"/>

如果不确定,请通过断点跟踪调试上的DataContext绑定值