2016-05-07 4 views
0

Wenn ich die Anwendung ausführen und das vmMain-Objekt initiiert wird, wird der Wert des Textfelds sampleCount und die Zeichenfolge "Test" auf der Konsole ausgedruckt, aber es wird nicht angezeigt die Ansicht (in der Textbox). Auch, wenn ich den Wert der Textbox (aus der Sicht) ändern, passiert nichts. Da die Eigenschaft nicht aktualisiert wird, wird in der Konsole nichts angezeigt.WPF-Bindung funktioniert nicht; Die Ansicht aktualisiert das Ansichtsmodell nicht und umgekehrt

MainwWindow.xaml:

<Window x:Class="WpfApplication3.MainWindow" 
     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:local="clr-namespace:WpfApplication3" xmlns:oxy="http://oxyplot.org/wpf" 
     xmlns:vm="clr-namespace:ViewModel;assembly=ViewModel" 
     Background="#FFDEDEDE" 
     WindowStyle="None" 
     AllowsTransparency="True" 
     WindowStartupLocation="CenterScreen" 

    mc:Ignorable="d" 
     Title="Compression Test" Height="1080" Width="1920"> 


    <Window.Resources> 
     <vm:MainViewModel x:Key="vmMain" 
     sampleCount="100" /> 
    </Window.Resources> 
    <Grid x:Name="gridUI"> 

     <StackPanel Orientation="Vertical"> 
      <StackPanel Height="100"> 

       <Border Background="#FF8986D3" Height="100" Margin="0,0,0,30" > 

        <TextBlock Text="COMPRESSION TEST" FontFamily="Sans-serif" FontSize="30" Foreground="#FFF9F9F9" VerticalAlignment="Center" FontWeight="Medium" HorizontalAlignment="Center"/> 

       </Border> 

      </StackPanel> 

      <StackPanel Orientation="Horizontal" Height="auto"> 
       <Border BorderBrush="White" BorderThickness="2" > 
       <StackPanel Orientation="Vertical" Width="200" Height="1080"> 


        <Label FontSize="24" FontFamily="Sans-serif" FontWeight="Medium" Name="doc" Foreground="White" Background="#FFA39AD8" Width="200" HorizontalContentAlignment="Center" Height="43">Files</Label> 
        <Border BorderBrush="#FFD4D4D4" BorderThickness="0.5" Grid.Row="3"></Border> 


        <StackPanel Name="sp_doc" Margin="0,10,0,0" > 
         <StackPanel Orientation="Horizontal" Name="sp_sample_button" Grid.Row="0" Grid.Column="0"> 
          <Image Source="pack://application:,,,/Resources/413.png" Height="40" Width="40" UseLayoutRounding="True" MouseDown="sampleDropDown" Cursor="Hand" Margin="5,0,0,0" Name="up_arrow"/> 
          <Image Source="pack://application:,,,/Resources/412.png" Height="40" Width="40" UseLayoutRounding="True" MouseDown="sampleDropDown" Cursor="Hand" Margin="5,0,0,0" Name="down_arrow" Visibility="Collapsed"/> 
          <!--<Button x:Name="sss" Click="sampleDropDown">s</Button>--> 
          <Label FontSize="18" FontFamily="Sans-serif" FontWeight="Light" Name="sam" Foreground="White" Margin="10">Samples</Label> 

         </StackPanel> 
         <StackPanel Orientation="Vertical" Name="sp_s"> 

         </StackPanel> 
         <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="1"> 
          <Image Source="pack://application:,,,/Resources/413.png" Height="40" Width="40" UseLayoutRounding="True" RenderTransformOrigin="-0.,0.558" MouseDown="reportDropDown" Cursor="Hand" Margin="5,0,0,0" Name="up_arrow1"/> 
          <Image Source="pack://application:,,,/Resources/412.png" Height="40" Width="40" UseLayoutRounding="True" Cursor="Hand" Margin="5,0,0,0" Name="down_arrow1" Visibility="Collapsed" MouseDown="reportDropDown"/> 
          <!--<Button Click="reportDropDown">r</Button>--> 
          <Label FontFamily="Sans-serif" FontWeight="Light" Foreground="White" FontSize="18" Margin="10">Reports</Label> 
         </StackPanel> 
         <StackPanel Orientation="Vertical" Name="sp_r"> 

         </StackPanel> 

        </StackPanel> 




       </StackPanel> 
       </Border> 
       <StackPanel Width="1781"> 
        <StackPanel Orientation="Horizontal" Background="#FFFDFDFD" Height="111"> 
         <TextBox Name="sampleCount" DataContext="{Binding sampleCount, Source={StaticResource vmMain}}" Width="200"></TextBox> 
         <Button Cursor="Hand" Height="75" Width="75" Style="{StaticResource CircleButton}" FontFamily="Sans-Serif" FontSize="25" Foreground="White" Click="NewSample_Click" Content="+" Margin="20,0,0,0" Background="#FFACAABF" /> 

         <StackPanel Margin="20,19,0,0"> 
          <Image Source="pack://application:,,,/Resources/file512.png" Height="75" Width="75" UseLayoutRounding="True" Margin="0,0,0,0" MouseDown="CreateReport_Click" Cursor="Hand" SnapsToDevicePixels="True"/> 
         </StackPanel> 

         <Image Source="pack://application:,,,/Resources/play1.png" Height="75" Width="75" UseLayoutRounding="True" Margin="20,18,0,18" MouseDown="CreateReport_Click" Cursor="Hand" SnapsToDevicePixels="True"/> 

         <Image Source="pack://application:,,,/Resources/1131.png" Height="75" Width="75" UseLayoutRounding="True" Margin="1340,0,0,0" MouseDown="CreateReport_Click" Cursor="Hand"/> 

        </StackPanel> 
        <Frame x:Name="newSampleFrame" Content="" HorizontalAlignment="center" VerticalAlignment="center" Width="934" Height="456" NavigationUIVisibility="Hidden" RenderTransformOrigin="0.408,0.5" Visibility="Collapsed"/> 
        <Frame x:Name="reportFrame" Content="" HorizontalAlignment="Center" Height="842" VerticalAlignment="Center" Width="595" Margin="0,100,0,0" NavigationUIVisibility="Hidden"/> 
        <Frame x:Name="graphFrame" Content="" HorizontalAlignment="Center" Height="456" VerticalAlignment="Center" Width="934" NavigationUIVisibility="Hidden" Visibility="Collapsed"/> 
       </StackPanel> 


      </StackPanel> 
     </StackPanel> 

    </Grid> 
</Window> 

MainViewModel.cs:

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 

    namespace ViewModel 
    { 
    public class MainViewModel : ObservableObject 
     { 
    public MainViewModel() 
    { 

    } 
    private int[] sampleName; 
    private string _sampleCount; 



    public int this[int pos] 
    { 
     get 
     { 
      return sampleName[pos]; 
     } 

     set 
     { 
      sampleName[pos] = value; 
     } 

    } 

    public string sampleCount 
    { 
     get 
     { 
      return _sampleCount; 

     } 
     set 
     { 
      if (value != _sampleCount) 
      { 
       _sampleCount = value; 
       OnPropertyChanged("sampleCount"); 
       Console.WriteLine("Test"); 
       Console.WriteLine(value); 
      } 
     } 
    } 

    } 
} 

ObservableObject.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Diagnostics; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ViewModel 
{ 
public abstract class ObservableObject : INotifyPropertyChanged 
{ 
    #region INotifyPropertyChanged Members 


    public event PropertyChangedEventHandler PropertyChanged; 


    protected virtual void OnPropertyChanged(string propertyName) 
    { 
     this.VerifyPropertyName(propertyName); 

     if (this.PropertyChanged != null) 
     { 
      var e = new PropertyChangedEventArgs(propertyName); 
      this.PropertyChanged(this, e); 
     } 
    } 

    #endregion // INotifyPropertyChanged Members 

    #region Debugging Aides 


    [Conditional("DEBUG")] 
    [DebuggerStepThrough] 
    public virtual void VerifyPropertyName(string propertyName) 
    { 

     if (TypeDescriptor.GetProperties(this)[propertyName] == null) 
     { 
      string msg = "Invalid property name: " + propertyName; 

      if (this.ThrowOnInvalidPropertyName) 
       throw new Exception(msg); 
      else 
       Debug.Fail(msg); 
     } 
    } 


    protected virtual bool ThrowOnInvalidPropertyName { get; private set; } 

    #endregion // Debugging Aides 
} 

} 

Antwort

2

Sie müssen die Eigenschaft Text binden:

Text={Binding sampleCount}

Verwandte Themen