2016-06-10 102 views
1

我沒有找到數據綁定無效的問題。我有兩個用戶控件。使用obervablecollection的用戶控件工作正常。綁定到對象的用戶控件不適用。如果我將值賦給文本,值確實會出現。在調試期間,我可以驗證這些值是否正確。
此邏輯遵循Paul Sheriff和本網站的一些帖子。 我的同事不在C#程序,所以他們不能幫助。我錯過了一些東西,但不知道它是什麼。Winrt Wpf databinding

從INotifyPropertyChanged的繼承

ViewModel類:

ParameterSettings _ps; 

    public ParameterSettings DetailData 
    { 
     get { return _ps; } 
     set 
     { 
      _ps = value; 
      RaisePropertyChanged("DetailData"); 
     } 
    } 

    public async Task GetParameters() 
    { 
     var pm = new ParameterManager(); 
     DetailData = new ParameterSettings(); 
     await pm.GetLoginCredentials(_ps); 
    } 

這是代碼的用戶控制。

ViewModels.ParameterSettingsVm _viewModel; 
    public ParameterSettingsUc() 
    { 
     this.InitializeComponent(); 
     _viewModel = (ParameterSettingsVm)Resources["viewModel"]; 
     var bounds = Window.Current.Bounds; 
     this.CancelBtn.Width = bounds.Width * .5; 
     this.SaveBtn.Width = bounds.Width * .5; 
    } 

    private async void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     await _viewModel.GetParameters(); 
     //UserNameBx.Text = _viewModel.DetailData.UserLogin; //textbox gets filled in. 
    } 


<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:SiteManager.Views" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:VM="using:SiteManager.ViewModels" 
    x:Class="SiteManager.Views.ParameterSettingsUc" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400" 
    Loaded="UserControl_Loaded"> 
    <UserControl.Resources> 
     <VM:ParameterSettingsVm x:Key="viewModel"></VM:ParameterSettingsVm> 
    </UserControl.Resources> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="50"/> 
     </Grid.RowDefinitions> 
     <Grid Grid.Row="0" > 
      <TextBox Header="Login:" VerticalAlignment="Center" Margin="2,10,0,0" Grid.Row="0" x:Name="UserNameBx" Text="{Binding Path=DetailData.UserLogin, Mode=TwoWay, UpdateSourceTrigger=Default}" > 
       <TextBox.DataContext> 
        <VM:ParameterSettingsVm/> 
       </TextBox.DataContext> 
      </TextBox> 
+0

您似乎有兩個'ParameterSettingsVm'實例 - 一個用於您的用戶控件的視圖模型,另一個用於'TextBox'的'DataContext'。這可能會造成一些混淆。此外,'ParameterSettings'是否實現'INotifyPropertyChange'並在UserLogin屬性上引發通知?你可以分享如何實施RaisePropertyChanged嗎? –

+0

另外 - 如果它是一個WinRT應用程序,那麼它不能是WPF。它可以是WPF或WinRT/XAML = Windows Store應用程序。 –

回答

0

我會改變是爲了使ViewModel屬性。

ViewModels.ParameterSettingsVm _viewModel {get;set;} 
    public ParameterSettingsUc() 
    { 
     this.InitializeComponent(); 
     _viewModel = (ParameterSettingsVm)Resources["viewModel"]; 
     var bounds = Window.Current.Bounds; 
     this.CancelBtn.Width = bounds.Width * .5; 
     this.SaveBtn.Width = bounds.Width * .5; 
    } 

    private async void UserControl_Loaded(object sender, RoutedEventArgs e) 
    { 
     await _viewModel.GetParameters(); 
     //UserNameBx.Text = _viewModel.DetailData.UserLogin; //textbox gets filled in. 
    } 

然後我將_viewModel設置爲textBox的數據上下文。哦,並將userControl的dataContext設置爲self,就像這樣。

<UserControl 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:SiteManager.Views" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:VM="using:SiteManager.ViewModels" 
    x:Class="SiteManager.Views.ParameterSettingsUc" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}" 
    Loaded="UserControl_Loaded"> 
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="*"/> 
      <RowDefinition Height="50"/> 
     </Grid.RowDefinitions> 
      <Grid Grid.Row="0" > 
       <TextBox Header="Login:" VerticalAlignment="Center" 
Margin="2,10,0,0" Grid.Row="0" x:Name="UserNameBx" 
Text="{Binding Path=DetailData.UserLogin, Mode=TwoWay, UpdateSourceTrigger=Default}" 
    DataContext={Binding _viewModel}> 
       </TextBox> 

雖然這可能不是你想要做的。我只是假設你正在創建_viewModel你想使用它。

+0

我曾經嘗試過,現在仍然沒有運氣。它就像我缺少一些基本的東西。謝謝你的嘗試。 – JenoS

0

來自微軟虛擬學院。使用x:將其綁定得更快,更簡潔。 mva.microsoft.com/en-US/training-courses/windows-10-data-binding-14579。我把每個類屬性都做成了vm的INotfiyChange屬性。

<UserControl 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
xmlns:local="using:SiteManager.Views" 
xmlns:VM="using:SiteManager.ViewModels" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:diag="using:System.Diagnostics" 
x:Class="SiteManager.Views.ParameterSettingsUc" 
mc:Ignorable="d" 
d:DesignHeight="300" 
d:DesignWidth="400" 
Loaded="UserControl_Loaded" > 
<UserControl.DataContext> 
    <VM:ParameterSettingsVm></VM:ParameterSettingsVm> 
</UserControl.DataContext> 
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="50"/> 
    </Grid.RowDefinitions> 
    <Grid Grid.Row="0" > 
     <Grid.RowDefinitions> 
      <RowDefinition Height="70"/> 
      <RowDefinition Height="70"/> 
      <RowDefinition Height="70"/> 
      <RowDefinition Height="70"/> 
     </Grid.RowDefinitions> 

     <TextBox Header="Login:" VerticalAlignment="Center" Margin="2,10,0,0" Grid.Row="0" x:Name="UserNameBx" Text="{x:Bind Path=_viewModel.UserLogin, Mode=TwoWay }" > </TextBox> 
     <TextBox Header="Password:" VerticalAlignment="Center" Margin="1" Grid.Row="1" x:Name="PasswordBx" Text="{x:Bind Path=_viewModel.UserPassword, Mode=TwoWay }"> </TextBox> 
     <TextBox Header="Mature Key:" VerticalAlignment="Center" Margin="1" Grid.Row="2" x:Name="MatureKeyBx" Text="{x:Bind Path=_viewModel.MatureKey, Mode=TwoWay }"> </TextBox> 


public sealed partial class ParameterSettingsUc : UserControl 
{ 
    ParameterSettingsVm _viewModel { get; set; } = new ParameterSettingsVm(); 


string _userLogin; 
    public string UserLogin 
    { 
     get { return _userLogin; } 
     set 
     { 
      _userLogin = value; 
      RaisePropertyChanged("UserLogin"); 
     } 
    }