0

私はWindows8アプリを作成していますが、ここ数日はカスタムユーザーコントロールで苦労しています。何が悪いのか本当にわかりません。

奇妙なことに、コードでSourceを変更すると、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);
        }
    }

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

PuzzleControl.xaml.cs

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

誰かが私のコードが間違っていることを知っていますか?Source = new ObservableCollection();を置くと PuzzleDataのコンストラクターでは、PropertyChangedイベントが発生します。DataContextに何かありますか?

事前にThnx!

4

2 に答える 2

1

よくわかりませんが、

しかし、あなたは設定します<controls:PuzzleControl Source="{Binding Path=PuzzleData}"/>

PuzzleData = List<string>

Source = ObservableCollection<string>

バインディングが初めて機能する場合でも(明らかに機能すること)、ソースがの<string>代わりに何らかの方法でリストに設定されている可能性がありObservableCollection<string>ます。これが、dependencypropertyメソッド(PropertyChanged)がに登録されているために呼び出されない理由である可能性がありますObservableCollection<string>

しかし、これはすべて純粋な推測であり、テストされていません。


彼のコードをレビューした後、PuzzleDataが実際に設定されたことがなく、それがエラーであることがわかりました...誤警報...。

于 2012-12-07T10:21:48.173 に答える
1

コンテキストをバインドしてよろしいですか? そして、どのようにオブジェクトをバインドしますか? グリッドビューのようにユーザー コントロールを使用すると、DataContext が変更され、ルート ページの Datacontext が異なります。

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

サブ コントロールを使用する場合は、ユーザー コントロール。次のように ElementName プロパティをバインドします。

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

よくわからない場合は、ブレークポイントを介してデバッグで DataContext バインディング値をトレースします。

于 2012-12-07T12:44:36.993 に答える