C# WPF绑定不起作用;视图不会更新viewmodel,反之亦然

C# WPF绑定不起作用;视图不会更新viewmodel,反之亦然,c#,wpf,data-binding,C#,Wpf,Data Binding,当我运行应用程序并启动vmMain对象时,textbox sampleCount的值和字符串“Test”会打印到控制台,但它不会显示在视图中(在textbox中)。 此外,每当我更改文本框的值时(从视图中),都不会发生任何事情。因为属性没有更新,并且控制台中没有显示任何内容 MainwWindow.xaml: <Window x:Class="WpfApplication3.MainWindow" xmlns="http://schemas.microsoft.co

当我运行应用程序并启动vmMain对象时,textbox sampleCount的值和字符串“Test”会打印到控制台,但它不会显示在视图中(在textbox中)。 此外,每当我更改文本框的值时(从视图中),都不会发生任何事情。因为属性没有更新,并且控制台中没有显示任何内容

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>
ObservieObject.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
}

}

您需要绑定文本属性:


Text={Binding sampleCount}

您需要绑定Text属性:

Text={Binding sampleCount}

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
}

}