2015-10-08 3 views
0

Je travaille sur le projet WPF. J'ai créé un nom de contrôle utilisateur comme "CanLogPaneView". ce contrôle utilisateur contient une zone de texte appelée "txt_CanLog". Je lie cette zone de texte comme mentionné ci-dessous:lier la zone de texte du contrôle de l'utilisateur par le texte mainwindow

<UserControl x:Class="CANCONTROL.CanLogPaneView" 
      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:local="clr-namespace:CANCONTROL" 
      xmlns:ViewModels="clr-namespace:CANCONTROL.ViewModels;assembly=CANCONTROL.ViewModels" 
      mc:Ignorable="d" 
      d:DesignHeight="300" d:DesignWidth="300"> 
    <UserControl.Resources> 
     <DataTemplate x:Key="MainWindowViewModel" DataType="{x:Type ViewModels:MainWindowViewModel}"> 
     </DataTemplate> 
    </UserControl.Resources> 
    <DockPanel> 
     **<TextBox x:Name="txt_CanLog" Text="{Binding Text, Source={StaticResource MainWindowViewModel}}" >** 

     </TextBox> 
    </DockPanel> 
</UserControl> 

J'ai donc lier la zone de texte avec le texte de la propriété MainWindow. Ma fenêtre principale a un modèle de vue. là, je défini le texte de la propriété comme mentionné ci-dessous:

public string text = string.Empty; 
public string Text 
     { 
      get 
      { 
       return text; 
      } 

      set 
      { 
       text = text + value; 
      } 
     } 

dans le code de la fenêtre principale: MainWindow.xaml.cs J'ajoutant le texte comme this.ViewModel.Text = "\ n \ nAPPLICATION CONFIGURATION \ r \ n" ;

Ce que je veux est par mainwindow.xaml.cs code que je veux imprimer des données dans le textBox de CanLogPaneView.xaml

Répondre

0

Votre MainWindowViewModel devrait être binded à votre DataContext de Usercontrol à la place. En outre, la mise en œuvre INotifyPropertyChanged dans votre MainWindowViewModel et RaisePropertyChange à votre "Texte" setter

Quelque chose comme le

suivant
<Window x:Class="WpfTestProj.MainWindow" 
    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" 
    xmlns:local="clr-namespace:WpfTestProj" 
    Title="MainWindow" Height="350" Width="525" 
    d:DataContext="{d:DesignInstance Type=local:MainViewModel, IsDesignTimeCreatable=False}"> 
<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="auto" /> 
    </Grid.RowDefinitions> 

    <TextBox Text="{Binding Text}" /> 
</Grid> 

public class MainViewModel : ViewModelBase 
{ 
    private string _text; 

    public string Text 
    { 
     get { return _text; } 
     set 
     { 
      _text = value; 
      OnPropertyChanged(); 
     } 
    } 
}