0

我正在創建一個Windows 8應用程序,並且我正在用自定義用戶控件掙扎最後幾天。我無法弄清楚什麼是錯的。UserControl Dependencyproperty不通過綁定更新

奇怪的是,當我在代碼中更改Source時,dependencyproperty調用了屬性changed事件,但綁定沒有更新。

因此,這裏是我的代碼:

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提前!

回答

1

我不知道肯定,

但你設置<controls:PuzzleControl Source="{Binding Path=PuzzleData}"/>

PuzzleData = List<string> 

Source = ObservableCollection<string> 

如果連結合的作品第一次(什麼apperantly做),那麼可能是這種情況下,源以某種方式設置爲列表<string>而不是ObservableCollection<string>。這可能就是你的依賴屬性方法(PropertyChanged)未被調用的原因,因爲它已註冊到ObservableCollection<string>

但這都是純粹的猜測,還沒有測試過它。


後,我得到了他的代碼審查是我發現的PuzzleData從來沒有真正建立,因此這是錯誤...誤報警....

+0

良好的通話,但它仍然不工作:\ \ –

1

確定綁定上下文?而如何綁定對象?如果你像在gridview中一樣使用你的用戶控件,DataContext被改變,並且不同的Datacontext根頁面。

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

如果你使用子控件,你的用戶控件;結合ElementName屬性是這樣的:

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

如果你不知道,跟蹤的DataContext通過斷點結合上調試值。

相關問題