2014-01-14 81 views
1

我一直在這一段時間工作,似乎無法找到我的問題的任何好的答案。我正在使用具有自定義依賴項屬性的自定義控件,並且在我的主應用程序中,我使用通過使用mvvmlight的視圖模型定位器查看的viewmodel綁定到這些propertys。我的問題是爲什麼綁定不更新,也沒有看到正確的datacontext?綁定不工作在自定義用戶控件的依賴屬性

代碼:

用戶控制的XAML:

<UserControl x:Name="zKeyBoard" 
     x:Class="ZLibrary.ZKeyBoard" 
     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" 
     DataContext="{Binding RelativeSource={RelativeSource Self}}" 
     d:DesignHeight="768" d:DesignWidth="1024"> 
<Grid> 
    <TextBox TextWrapping="Wrap" Text="{Binding zDisplayText}" /> 
    <Label Content="{Binding zBoxToEdit}"/> 
</Grid> 
</UserControl> 

事情我都試圖在用戶控制的XAML不已:

<TextBox TextWrapping="Wrap" Text="{Binding zDisplayText, ElementName=zKeyBoard}" /> 
<Label Content="{Binding zBoxToEdit, ElementName=zKeyBoard}"/> 

用戶控制C#:

using System.ComponentModel; 

namespace ZLibrary 
{ 

public partial class ZKeyBoard : UserControl, INotifyPropertyChanged 
{ 

    public ZKeyBoard() 
    { 
     InitializeComponent(); 
    } 

public string zBoxToEdit 
    { 
     get { return (string)GetValue(zBoxToEditProperty); } 
     set { SetValue(zBoxToEditProperty, value); } 
    } 

    public static readonly DependencyProperty zBoxToEditProperty = 
     DependencyProperty.Register("zBoxToEdit", typeof(string), typeof(ZKeyBoard), new UIPropertyMetadata("")); 

public string zDisplayText 
    { 
     get { return (string)GetValue(zDisplayTextProperty); } 
     set { SetValue(zDisplayTextProperty, value); } 
    } 

    public static readonly DependencyProperty zDisplayTextProperty = 
     DependencyProperty.Register("zDisplayText", typeof(string), typeof(ZKeyBoard), new UIPropertyMetadata("")); 
} 

} 

事情我已經樹立了d用戶控件C#:

public string zBoxToEdit 
    { 
     get; 
     set; 
    } 

public string zDisplayText 
    { 
     get; 
     set; 
    } 

這裏的項目文件,其中用戶控件正在使用:

的App.xaml:

<Application x:Class="WpfApplication1.App" 
     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:vm="clr-namespace:Sandstorm.ViewModel" 
     mc:Ignorable="d" 
     StartupUri="Main.xaml"> 
<Application.Resources> 
    <ResourceDictionary> 
     <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" /> 
    </ResourceDictionary> 
</Application.Resources> 
</Application> 

視圖模型定位:

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Ioc; 
using Microsoft.Practices.ServiceLocation; 

namespace Sandstorm.ViewModel 
{ 
class ViewModelLocator 
{ 
    public ViewModelLocator() 
    { 
     ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default); 

     SimpleIoc.Default.Register<KeyBoardViewModel>(() => 
     { 
      return new KeyBoardViewModel(); 
     }); 
    } 

    public KeyBoardViewModel KeyBoardViewModel 
    { 
     get { return ServiceLocator.Current.GetInstance<KeyBoardViewModel>(); } 
    } 

    public static void Cleanup() 
    { 
     // TODO Clear the ViewModels 
    } 
} 

} 

The Xaml The User Control is In Used In:

<Page x:Name="keyboard_Frame" 
    x:Class="Sandstorm.keyBoard" 
    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" 
    xmlns:ZControls="clr-namespace:ZLibrary;assembly=ZLibrary" 
    DataContext="{Binding KeyBoardViewModel, Source={StaticResource Locator}}" 
    mc:Ignorable="d" 
    d:DesignHeight="768" d:DesignWidth="1024" 
    ShowsNavigationUI="False" 
Title="KeyBoard"> 
<Grid> 
    <ZControls:ZKeyBoard zBoxToEdit="{Binding boxToEdit}" zDisplayText="{Binding keyboardEntry}" /> 
</Grid> 
</Page> 

當這個Xaml按原樣運行這種方式它在控制檯拋出一個錯誤,它說它找不到boxToEdit或keyboarEntry的綁定屬性,並且它將原始的ZKeyBoard Name作爲無法找到的地方...所以我加了這一點:

<ZControls:ZKeyBoard zBoxToEdit="{Binding boxToEdit, RelativeSource={RelativeSource Mode=TemplatedParent}}" zDisplayText="{Binding keyboardEntry, RelativeSource={RelativeSource Mode=TemplatedParent}}" /> 

造成錯誤走開我假設意味着它可以找到視圖模型,但仍沒有動靜。

終於視圖模型:

using GalaSoft.MvvmLight; 
using GalaSoft.MvvmLight.Command; 
using System.ComponentModel; 

namespace Sandstorm.ViewModel 
{ 

class KeyBoardViewModel : ViewModelBase, INotifyPropertyChanged 
{ 
    private string _keyboardEntry; 
    private string _boxToEdit; 

public KeyBoardViewModel() 
    { 
     _boxToEdit = "yay"; 
    } 

public event PropertyChangedEventHandler PropertyChanged; 

    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     if (this.PropertyChanged != null) 
     { 
      this.PropertyChanged(this, 
       new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

public string keyboardEntry 
    { 
     get { return this._keyboardEntry; } 
     set 
     { 
      if (this._keyboardEntry != value) 
      { 
       this._keyboardEntry = value; 
       this.OnPropertyChanged("keyboardEntry"); 
       Console.Out.WriteLine(this._keyboardEntry); 
      } 
     } 
    } 

    public string boxToEdit 
    { 
     get { return this._boxToEdit; } 
     set 
     { 
      if (this._boxToEdit != value) 
      { 
       this._boxToEdit = value; 
       this.OnPropertyChanged("boxToEdit"); 
       Console.Out.WriteLine(this._boxToEdit); 
      } 
     } 
    } 

} 

} 

一件事我注意到的是,我不能看到Console.out.writeline做任何這對我來說意味着它不是設置在所有。所以很多重要的問題,爲什麼這不起作用。任何關於這個的幫助都會很棒!它可能是小而愚蠢的,但第二雙眼睛可能會比我更快地注意到它。

+2

不要將DataContext設置爲self。問題已解決。 –

+0

好吧,現在試試吧 – Keleko

+0

omg謝謝,這麼小的東西,但打破了一切,我從來沒有嘗試過。 TY! – Keleko

回答

4

簡單的回答:

不要將DataContext設置爲self。

問題解決

相關問題