子页面刷新父页面

时间:2012-02-16 02:27:37

标签: wpf

我想知道是否有办法触发父页面。例如,我从父页面下拉,当我选择一个项目时,这将打开一个“子”,另一个窗口基于我选择的内容。我想在这里实现的是,在子窗口中,我可以根据单选按钮选择一个项目,这应该自动更新父窗口以获得正确的文本。我不知道这是否有意义,但我希望有人能够理解我所处的位置并尽可能地帮助他们。

2 个答案:

答案 0 :(得分:1)

一种选择是在子类中创建要绑定的文本的依赖项属性。然后,您可以绑定到所选项目以访问所述属性。

这是开始使用它的地方  http://msdn.microsoft.com/en-us/library/ms750428.aspx

为了更进一步,您可以创建一个基本控件来将依赖属性放在所有子控件都将继承的位置。

编辑 基类

public class BaseExample : ContentControl
{
    public string BaseText
     {
         get { return (string)this.GetValue(BaseTextProperty); }
         set { this.SetValue(BaseTextProperty, value); } 
     }
      public static readonly DependencyProperty StateProperty = DependencyProperty.Register(
"BaseText", typeof(string), typeof(BaseExample),new PropertyMetadata(false));  
}


public class child : BaseExample { }

从这里你将继承你的控件类似于

 <local:BaseExample  
 x:Class="child" 
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:local="YourAssembly"> </localBaseExample>

答案 1 :(得分:0)

由于路由事件可能会冒泡可视树,因此请利用SelectionChanged中的Selector事件。我假设有两件事:

  • 您使用的是ComboBox(源自Selector
  • 您的子视图嵌套在父视图中

如果这些假设成立,你可以这样做:

家长视图

<UserControl
    x:Class="Views.ParentView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:v="clr-namespace:Views">
    <!-- you can handle bubbled events outside the control of where it originated -->
    <Grid
        Selector.SelectionChanged="Grid_SelectionChanged">
        <v:ChildView
            x:Name="Child" />
    </Grid>
</UserControl>

家长视图代码

using System.Windows;
using System.Windows.Controls;

namespace Views
{
    public partial class ParentView : UserControl
    {
        public ParentView()
        {
            InitializeComponent();
        }

        private void Grid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // because selectors allow multiple selections, a list is used.
            foreach (object selectedItem in e.AddedItems)
            {
                // do stuff with your selected items
            }
        }
    }
}

子视图

<UserControl
    x:Class="Views.ChildView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <ComboBox>
            <ComboBoxItem>One</ComboBoxItem>
            <ComboBoxItem>Two</ComboBoxItem>
            <ComboBoxItem>Three</ComboBoxItem>
        </ComboBox>
    </Grid>
</UserControl>
相关问题