UserControl绑定无效

时间:2016-09-09 09:39:21

标签: c# wpf xaml mvvm user-controls

我正在尝试在我的WPF应用程序上创建一个简单的UserControl。 这是MainWindow.xaml

 <Window x:Class="PresentationLoginTest.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:PresentationLoginTest"
        xmlns:userLocal="clr-namespace:PresentationLoginTest.Toolkit;assembly=PresentationLoginTest.Toolkit"
        mc:Ignorable="d"
        x:Name="mainWindow"
        Title="MainWindow" Height="350" Width="525">
    <Grid>


        <ItemsControl x:Name="tStack" ItemsSource="{Binding Agents}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <WrapPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel><
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <userLocal:Utilisateur Test="{Binding FirstName, Mode=TwoWay}"/>

                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>


    </Grid>
</Window>

以下是Utilisateur.xaml

<UserControl x:Class="PresentationLoginTest.Toolkit.Utilisateur"
             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:PresentationLoginTest.Toolkit"
             DataContext="ViewModel.UtilisateurViewModel"
             mc:Ignorable="d" 
             x:Name="self"
             d:DesignHeight="107.902" d:DesignWidth="318.801">
    <Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="1*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
        <TextBlock Text="{Binding Test, ElementName=self}" Background="#FFFF1818" />
        <TextBlock Grid.Column="1">Nom_Agent</TextBlock>
        <Button  Grid.Column="2"  >Connection</Button>
    </Grid>

</UserControl>

以下是Utilisateur的代码:

public partial class Utilisateur : UserControl
    {
        private UtilisateurViewModel _vm;

        public Utilisateur()
        {

            InitializeComponent();
            _vm = new UtilisateurViewModel();
            DataContext = _vm;

        }

        public static readonly DependencyProperty TestProperty =
       DependencyProperty.Register("Test",
                                   typeof(string),
                                   typeof(Utilisateur),
                                    new PropertyMetadata(null, OnCaptionPropertyChanged));
        public string Test
        {
            get { return (String)GetValue(TestProperty); }
            set { SetValue(TestProperty, value); }
        }


        private static void OnCaptionPropertyChanged(DependencyObject dependencyObject,
                DependencyPropertyChangedEventArgs e)
        {
            Utilisateur myUserControl = dependencyObject as Utilisateur;
            //myUserControl.TB.Text = "test";
            (myUserControl.DataContext as UtilisateurViewModel).Test = myUserControl.Test;
        }

    }

UtilisateurViewModel

class UtilisateurViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            var handler = PropertyChanged;
            handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        private string _test;
        public string Test
        {
            get { return _test; }
            set { _test = value; OnPropertyChanged("Test"); }
        }

    }

我不明白为什么这不起作用,有人可以帮我吗?

这里可选择加载我的“代理人”的MainViewModel

public class MainViewModel : ViewModelBase, INotifyPropertyChanged
{
    private ObservableCollection<Agent> _agents;
    public ObservableCollection<Agent> Agents
    {
        get
        {
            if (_agents == null)
                ChargementAgent();
            return _agents;
        }
        set
        {
            _agents = value;
            RaisePropertyChanged();
            OnPropertyChanged("Agents");
        }
    }

    private string _test;
    public string Test
    {
        get { return _test; }
        set
        {
            _test = value;
            OnPropertyChanged("Test");
        }
    }

    public void ChargementAgent()
    {
        try
        {
            var aagents = new ObservableCollection<Agent>();

            for (int i = 0; i < 10; i++)
            {

                var agent = new Agent("Prénom_Agent_" + i, "Nom_Agent_" + i);
                aagents.Add(agent);
            }
            this.Agents = aagents;

            this.Test = "yes";
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message, "ErrorLoadingAgents", MessageBoxButton.OK, MessageBoxImage.Error);
        }

    }


    public new event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        handler?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }



}

0 个答案:

没有答案