2

我怎样才能从 aUserControl和父级访问数据/属性MainWindow(反之亦然)?例如,假设我的TextBoxMainWindow调用中有一个mainTextBox。然后我UserControl用另一个TextBoxucTextBox. 我还有一个名为的按钮ucButtonUserControl应该弹出一个MessageBox带有值乘积的按钮mainTextBox.Text * ucTextBox.Text(转换为双精度,使其工作)。

我真正想知道的是如何通过一个按钮来动态地实现这一点,该按钮允许创建更多UserControls能够与父级交互的按钮。在这种情况下,命名每个UserControl.

我尝试了几件事,主要是 get,set 属性,但没有理想的结果。我不确定是否需要使用UserControl,但似乎,我已经读过这CustomControl是为了进行深度定制,但我不需要。

4

1 回答 1

3

这只是一个快速示例,可以帮助您入门(@Adriano 先生可能是什么意思):

RootViewModel.cs

public class RootViewModel :INotifyPropertyChanged
{
    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged = delegate {};

    private void OnPropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    private double _x;
    private double _y;

    public double X
    {
        get { return _x; }
        set
        {
            _x = value;
            OnPropertyChanged("X");
        }
    }

    public double Y
    {
        get { return _y; }
        set
        {
            _y = value;
            OnPropertyChanged("Y");
        }
    }

    public double XY
    {
        get { return _x * _y; }
    }
}

用户控件1.xaml

<UserControl x:Class="WpfApplication2.UserControl1"
         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:DesignWidth="200">
<Grid>
    <GroupBox Header="User Control">
        <StackPanel>
            <Label Content="Y:" />
            <TextBox Text="{Binding Path=Y, UpdateSourceTrigger=PropertyChanged, FallbackValue=1}" Margin="5" />
            <Button Content="Press me" Click="OnButtonClick" />
        </StackPanel>
    </GroupBox>
</Grid>

UserControl1.xaml.cs

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    private void OnButtonClick(object sender, RoutedEventArgs e)
    {
        var viewModel = (RootViewModel)DataContext;
        var resultMessage = string.Format("{0} * {1} = {2}", viewModel.X, viewModel.Y, viewModel.XY);

        MessageBox.Show(resultMessage, "X * Y");
    }
}

MainWindow.xaml:

<Window x:Class="WpfApplication2.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WpfApplication21="clr-namespace:WpfApplication2"
    Title="Main Window" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition />
    </Grid.RowDefinitions>
    <StackPanel>
        <Label Content="X:" />
        <TextBox Text="{Binding Path=X, UpdateSourceTrigger=PropertyChanged, FallbackValue=1}" Margin="5" Height="24" />
    </StackPanel>
    <WpfApplication21:UserControl1 Grid.Row="1" Margin="5" />
</Grid>

MainWindow.xaml.cs:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = new RootViewModel
        {
            X = 5,
            Y = 7
        };
    }
}
于 2013-06-03T14:03:12.483 回答