如何从usercontrol获取值

时间:2014-07-03 03:38:00

标签: wpf

这可能是一个简单的问题,但我是WPF和C#的新手。   我的问题的背景:我有几个用户控件用作配置菜单,其中一个一次显示。每个usercontrol都有很多复选框,文本框和combox。现在,我想将usercontrols中的值传递给mainwindow。   我该怎么办?我真的很担心这个问题。请帮我!!

MainWindow.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:local="clr-namespace:WpfApplication3"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Button Content="Start" Margin="103,12,315,273" Click="Start_Click" />
         <!--<Frame Name="myframe"/>-->
        <local:UserControl1 x:Name="myUserControlInstance1" Height="200" Width="180" />
        <local:MyUserControl x:Name="myUserControlInstance" Height="200" Width="180"/>
    <Button Content="ShowContent" Height="23" HorizontalAlignment="Left" Margin="294,15,0,0" Name="ShowContent" VerticalAlignment="Top" Width="85" Click="Show_Click" />
</Grid>
</Window>

MainWindow.xaml.cs

public partial class MainWindow : Window
{
    public bool btn_ConfigTest = false;
    public bool btn_ConfigTest2 = false;
    public bool Flag = true;
    public MainWindow()
    {
        InitializeComponent();
    }


    private void Start_Click(object sender, RoutedEventArgs e)
    {
    }

    private void Show_Click(object sender, RoutedEventArgs e)
    {
        string myvalue1 = myUserControlInstance1.Ischecked;
        //myframe.Content = new UserControl1();
        string myvalue2 = myUserControlInstance.Value;
    } 
}

Myusercontrol.xaml.cs

public partial class MyUserControl : UserControl
{
    DependencyModel mymodel = new DependencyModel();
    public MyUserControl()
    {
        InitializeComponent();
        //this.DataContext = mymodel;
    }

    public string Value
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
    public static readonly DependencyProperty ValueProperty =
        DependencyProperty.Register("Value", typeof(string),
          typeof(MyUserControl), new PropertyMetadata("abc"));


    private void button1_Click(object sender, RoutedEventArgs e)
    {
        //double Result = mymodel.Value;
        //mymodel.Value = 1234;
    }

}

Myusercontrol.xaml

<UserControl x:Class="WpfApplication3.MyUserControl"
         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" 
         x:Name="parent"
         d:DesignHeight="300" d:DesignWidth="300">
<Grid Background="#FFD4CE61" Height="200" Width="180" DataContext="{Binding ElementName=parent}">
    <TextBox Height="27" HorizontalAlignment="Left" Margin="62,63,0,0" x:Name="mytextBox1" VerticalAlignment="Top" Width="64" Text="{Binding Path=Value,Mode=TwoWay}" />
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="51,12,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <CheckBox Content="CheckBox1" Height="16" HorizontalAlignment="Left" Margin="51,109,0,0" x:Name="mycheckBox1" VerticalAlignment="Top" IsChecked="{Binding Path=IsChecked,Mode=TwoWay}"/>
</Grid>
</UserControl>

Usercontrol1.xaml

<UserControl x:Class="WpfApplication3.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" 
         x:Name="parent"
         d:DesignHeight="300" d:DesignWidth="300">


<Grid Background="#FF793D6F" Height="200" Width="180" DataContext="{Binding ElementName=parent}">
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="51,33,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    <CheckBox Content="CheckBox" Height="16" HorizontalAlignment="Left" Margin="51,85,0,0" Name="mycheckBox1" VerticalAlignment="Top" IsChecked="{Binding Path=Ischecked}"/>
</Grid>
</UserControl>

Usercontrol1.xaml.cs

public partial class UserControl1 : UserControl
{
    //DependencyModel mymodel = new DependencyModel();
    public UserControl1()
    {
        InitializeComponent();
        //this.DataContext = mymodel;

    }

    public string Ischecked
    {
        get { return (string)GetValue(ValueProperty); }
        set { SetValue(ValueProperty, value); }
    }
    public static readonly DependencyProperty ValueProperty =
    DependencyProperty.Register("Ischecked", typeof(string),
      typeof(UserControl1), new PropertyMetadata("true"));


    public void button1_Click(object sender, RoutedEventArgs e)
    {
    }
}

2 个答案:

答案 0 :(得分:1)

步骤1:对于用户控件中的每个控件,您需要在代码后面创建一个属性。在get块中,您将返回要在主窗口中访问的属性。

第2步:构建项目。在主窗口中添加用户控件

步骤3:当您在主窗口中使用用户控件时,在后面的代码中您可以设置usecontrol-name(在mainwindow.xaml中).propert-name(在用户控件后面的代码中创建的属性)= value-you-希望对集合

答案 1 :(得分:-1)

我认为.xaml.cs中的每个公共财产都可以公开访问。如果你想从xaml获取值,那么最安全的方法是使公共属性访问xaml(首先命名xaml控件)。你不需要依赖属性来这样做。

以下是我自己项目的示例。它不符合您的OP,但我希望您能理解这一点。

XAML

<UserControl ...>
<UserControl.Resources>
    ...
</UserControl.Resources>
<Grid ...>
    <Grid.ColumnDefinitions>
        ...
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" x:Name="octaveTopRow"/>
        <RowDefinition Height="Auto" x:Name="noteRow"/>
        <RowDefinition Height="Auto" x:Name="octaveBotRow"/>
    </Grid.RowDefinitions>
    ...
</Grid>

XAML.CS

public partial class NotationBox : UserControl
{
    public double TotalBotHeight
    {
        get { return octaveBotRow.ActualHeight; }
    }

    public NotationBox()
    {
        InitializeComponent();
    }
    ...
}

希望得到这个帮助。