-1

我在 WPF 中为 Windows 10 应用程序创建了一个自定义控件。问题是主类中的绑定不起作用。它绑定到我的自定义控件。任何人都可以看到问题吗?我该如何修复代码。它不起作用。特别是绑定不起作用。我该如何解决。我不知道如何解决这个问题。

查看型号代码

         using System;
        using System.Collections.Generic;
        using System.ComponentModel;
        using System.Linq;
        using System.Runtime.CompilerServices;
        using System.Text;
        using System.Threading.Tasks;
        using Windows.UI.Xaml;
        using AppDemo.Annotations;

        namespace AppDemo
        {
            public class ViewModel : INotifyPropertyChanged
            {
                private String t1, t2;
                public String T1
                {
                    get { return t1; }
                    set
                    {
                        t1 = value;
                        Concat = T1 + T2;
                    }
                }
                public String T2
                {
                    get { return t2; }
                    set
                    {
                        t2 = value;
                        Concat = T1 + T2;
                    }
                }

                private String concat;
                public String Concat
                {
                    get { return concat; }
                    set
                    {
                        concat = value;
                        OnPropertyChanged(nameof(Concat));
                    }
                }



                public event PropertyChangedEventHandler PropertyChanged;

                [NotifyPropertyChangedInvocator]
                protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
                {
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));


                }
            }
        }
    <UserControl
        x:Class="AppDemo.ExampleControl"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:AppDemo"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        d:DesignHeight="300"
        d:DesignWidth="400">

        <Grid Name="grid">
           <StackPanel>
               <TextBox Text="{Binding T1,Mode=TwoWay}"/>
               <TextBox Text="{Binding T2,Mode=TwoWay}"/>
            </StackPanel>
        </Grid>
    </UserControl>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using AppDemo.Annotations;

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236

namespace AppDemo
{
    public sealed partial class ExampleControl : UserControl
    {
        public static readonly DependencyProperty ConcatProperty = DependencyProperty.Register(
            "Concat", typeof(String), typeof(ExampleControl), new PropertyMetadata(default(String)));


        private ViewModel m;

        public String Concat
        {
            get { return (String)GetValue(ConcatProperty); }
            set { SetValue(ConcatProperty, value); }
        }

        public ExampleControl()
        {
            this.InitializeComponent();
            m = new ViewModel();
            grid.DataContext = m;
            m.PropertyChanged += M_PropertyChanged;
        }

        private void M_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            Concat = m.Concat;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}



<Page
    x:Class="AppDemo.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:AppDemo"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <local:ExampleControl Concat="{Binding C}"/>
        <Button Name="btnTest" Click="BtnTest_OnClick">Test</Button>
        </StackPanel>
    </Grid>
</Page>

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409

namespace AppDemo
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public String C { get; set; }

        public MainPage()
        {
            this.InitializeComponent();
            this.DataContext = this;
        }


        private void BtnTest_OnClick(object sender, RoutedEventArgs e)
        {
            String msg = C;
        }
    }
}
4

2 回答 2

0

通常最好将您想要绑定的属性放在它们自己独特的视图模型类中 - 不要与您的视图代码混合。这段代码不必要地迟钝。

顺便说一句...您的 UserControl 名为 ExampleControl,具有其 Text 属性绑定到 T1 和 T2 的文本框。但是,当您设置其中任何一个值时,它们会设置 Concat 属性,但无法使用自己的名称引发 PropertyChanged 事件。你可以解决这个问题,例如:

public String T1
{
  set
  {
    t1 = value;
    OnPropertyChanged(nameof(T1));
    Concat = T1 + T2;
  }
}
于 2018-07-12T07:04:12.173 回答
0

在你Mode的: Binding_TwoWayMainPage

<local:ExampleControl Concat="{Binding C, Mode=TwoWay}"/>
于 2018-07-12T09:50:19.443 回答