简体   繁体   English

WPF绑定不起作用; 视图未更新视图模型,反之亦然

[英]WPF binding is not working; the view is not updating the viewmodel and vice versa

When I run the application and the vmMain object is initiated, the value of the textbox sampleCount and the string "Test" is printed out to the console, but it doesn't show in the view (in the textbox ). 当我运行该应用程序并启动vmMain对象时,文本框sampleCount和字符串“ Test”的值会打印到控制台,但不会显示在视图中(在textbox中)。 Also, Whenever I change the value of the textbox (from the view), nothing happen. 另外,无论何时(从视图中)更改文本框的值,都不会发生。 As the property is not updated and nothing shows in the console. 由于该属性未更新,因此控制台中未显示任何内容。

MainwWindow.xaml: 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 : 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 : 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
}

}

您需要绑定Text属性:

Text={Binding sampleCount}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM