需要帮助使一个UserControl中的按钮使用C#中的Properties更新另一个User控件中的GUI

本文关键字:Properties 中的 更新 另一个 GUI 控件 User 按钮 一个 UserControl 帮助 | 更新日期: 2023-09-27 18:29:03

我正在尝试编写一个程序,如果您单击一个用户控件中的列表框项目或按钮,它将更新另一个用户控制中的文本框。我似乎不知道如何使用dependencyproperty使其正常工作。

Listbox.xaml

<UserControl x:Class="TestDP3.ListBoxUserControl"
         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:local="clr-namespace:TestDP3">
<UserControl.DataContext>
    <local:MyViewModel/>
</UserControl.DataContext>
<Grid>
    <ListBox x:Name="lstbox" HorizontalAlignment="Left" Height="144" Margin="21,23,0,0" VerticalAlignment="Top" Width="149" SelectionChanged="Selector_OnSelectionChanged"/>
    <Button Content="Button" HorizontalAlignment="Left" Margin="105,231,0,0" VerticalAlignment="Top" Width="75" Command="{Binding TestCommand}"/>
    <TextBlock HorizontalAlignment="Left" Margin="52,272,0,0" TextWrapping="Wrap" Text="{Binding MyDp.Result}" VerticalAlignment="Top"/>
</Grid>

VnInfo.xaml

<UserControl x:Class="TestDP3.VnInfoUserControl"
             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" 
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300"
             xmlns:local="clr-namespace:TestDP3">
    <UserControl.DataContext>
        <local:MyViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBlock HorizontalAlignment="Left" Margin="10,102,0,0" TextWrapping="Wrap" Text="DependencyProperty is: " VerticalAlignment="Top"/>    
        <TextBlock HorizontalAlignment="Left" Margin="159,102,0,0" TextWrapping="Wrap" Text="{Binding MyDp.Result, Mode=TwoWay}" VerticalAlignment="Top"/>
    </Grid>
</UserControl>

MyViewModel.cs

public class MyViewModel : INotifyPropertyChanged
    {
        private MyDP _myDP;
        public event PropertyChangedEventHandler PropertyChanged;
        public MyViewModel()
        {
            _myDP = new MyDP();
            TestCommand = new MyCommand(SampleMethod);
        }
        public MyDP MyDp
        {
            get { return _myDP; }
            set
            {
                _myDP = value;
                RaisePropertyChanged("MyDP");              
            }
        }
        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        public ICommand TestCommand { get; set; }
        private int counter = 0;
        private int SampleMethod()
        {
            MyDp.Result = counter++;
            return MyDp.Result;  
        }
    }

MyDP.cs

public class MyDP : DependencyObject
    {
        public int Result
        {
            get { return (int)GetValue(ResultProperty); }
            set { SetValue(ResultProperty, value); }
        }
        public static readonly DependencyProperty ResultProperty =
            DependencyProperty.Register("Result", typeof(int), typeof(MyDP) ,new PropertyMetadata(0) 
            );        
    }

以下是视觉工作室项目的链接:https://dl.dropboxusercontent.com/u/22398345/TestDP.zip

需要帮助使一个UserControl中的按钮使用C#中的Properties更新另一个User控件中的GUI

两个视图必须访问同一对象(ViewModel)。我怀疑你打给的电话

<local:MyViewModel/>

在每个Xaml文件中创建单独的对象。您需要在代码背后的某个地方创建一个MyViewModel对象:

MyViewModel vm = new MyViewModel();

并通过构造函数将其引用传递给每个视图:

ListBoxUserControl(MyViewModel vm) { ... }

或到一处房产:

ListBoxUserControl.MyViewModel = vm;

然后,您可以在后面的代码中设置DataContext:

this.DataContext = vm;