如何在WPF中使用Buttons / Code Behind实例同步或访问2个UserControl?

时间:2012-06-10 15:21:49

标签: c# wpf user-controls

我的项目有问题。这是我的解决方案资源管理器:

enter image description here

BLA是一个示例UserControl,它被加载到MainWindow Grid中两次:

<Window x:Class="UserControlWechseln.MainWindow"
        xmlns:local="clr-namespace:UserControlWechseln"
        Title="MainWindow" Height="428" Width="1195" xmlns:am="http://schemas.amcharts.com/charts/wpf/2009/xaml">
    <Grid Height="1000" Width="1000">            
        <Grid Height="500" HorizontalAlignment="Left" Margin="12,31,0,0" Name="grid1" VerticalAlignment="Top" Width="142">
            <local:BLA Margin="-3,-17,-270,17" />
        </Grid> 

        <Grid Height="500" HorizontalAlignment="Right" Margin="0,6,31,0" Name="grid2" VerticalAlignment="Top" Width="402">
            <local:BLA />
        </Grid>    
    </Grid>
</Window>

BLA UserControl的XAML代码如下所示:

<UserControl x:Class="UserControlWechseln.BLA"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid Name="Bla">
        <Button Content="House" Height="23" HorizontalAlignment="Left" Margin="64,237,0,0" Name="button1" VerticalAlignment="Top" Width="75"  Click="ButtonClick" />
        <Button Content="Soccer" Height="23" HorizontalAlignment="Left" Margin="169,237,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="ButtonClick" />    
    </Grid>
</UserControl>

BLA UserControl的C#代码如下所示:

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

        private void ButtonClick(object sender, RoutedEventArgs e) {
            Button btn = sender as Button;
            Bla.Children.Clear();

            if (btn.Content.ToString() == "House") {
                Haus uc1 = new Haus();
                Bla.Children.Add(uc1);
            } else if (btn.Content.ToString() == "Soccer") {
                Fussball uc2 = new Fussball();
                Bla.Children.Add(uc2);                
            }
        }

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

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

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

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

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

现在的问题是你得到了这个观点:

enter image description here

是否可以触发Home按钮一次,并且Img in显示在两个UserControls“BLA”上?它们显然是相同的UserControl。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

它们是UserControl的不同实例,它们不知道另一个存在。您需要创建PropertiesEvents。我确信它可能有更好的方法但是这里是我一起演示的东西(我也重命名了你的一些类,因为我没有你所拥有的相同的命名空间)。当您更改图像时,还要清除UserControl中的所有控件。

修改添加了完整示例

<强>用户控件

namespace WpfApplication1{

    /// <summary>
    /// Interaction logic for Bla.xaml
    /// </summary>
    public partial class Bla : UserControl
    {
        public event RoutedEventHandler changeHaus
        {
            add { AddHandler(myEvents.changeHausEvent, value); }
            remove { RemoveHandler(myEvents.changeHausEvent, value); }
        }

        public event RoutedEventHandler changeSoccer
        {
            add { AddHandler(myEvents.changeSoccerEvent, value); }
            remove { RemoveHandler(myEvents.changeSoccerEvent, value); }
        }

        public Bla()
        {
            InitializeComponent();
        }
        private void ButtonClick(object sender, RoutedEventArgs e) { 
            Button btn = sender as Button; 
            if (btn.Content.ToString() == "House")
            {
                SetHaus();
                RoutedEventArgs ea = new RoutedEventArgs(myEvents.changeHausEvent);
                RaiseEvent(ea);
            }
            else if (btn.Content.ToString() == "Soccer")
            {
                SetSoccer();
                RoutedEventArgs ea = new RoutedEventArgs(myEvents.changeSoccerEvent);
                RaiseEvent(ea);
            }
        } 

        public void SetHaus()
        {
            display.Fill = new SolidColorBrush(Colors.Blue);
        }

        public void SetSoccer()
        {
            display.Fill = new SolidColorBrush(Colors.Red);
        }
    }
    public static class myEvents
    {
        public static  RoutedEvent changeHausEvent = EventManager.RegisterRoutedEvent("changeHaus",RoutingStrategy.Tunnel,typeof(RoutedEventHandler), typeof(Bla));
        public static RoutedEvent changeSoccerEvent = EventManager.RegisterRoutedEvent("changeSoccer", RoutingStrategy.Tunnel, typeof(RoutedEventHandler), typeof(Bla));

    }
}

UserControl Xaml

<UserControl x:Class="WpfApplication1.Bla"
         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">
    <Grid Name="bla">
        <Grid.RowDefinitions>
            <RowDefinition Height="3*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Rectangle Name="display" Height="223" Grid.Row="0" />
        <Button Grid.Row="1" Content="House" Height="23" HorizontalAlignment="Left" Margin="64,25,0,0" Name="button1" VerticalAlignment="Top" Width="75"  Click="ButtonClick" />
        <Button Grid.Row="1" Content="Soccer" Height="23" HorizontalAlignment="Left" Margin="169,25,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="ButtonClick" />
    </Grid>

</UserControl>

主窗口

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void bla1_changeHaus(object sender, RoutedEventArgs e)
        {
            bla2.SetHaus();
        }

        private void bla1_changeSoccer(object sender, RoutedEventArgs e)
        {
            bla2.SetSoccer();
        }

        private void bla2_changeHaus(object sender, RoutedEventArgs e)
        {
            bla1.SetHaus();
        }

        private void bla2_changeSoccer(object sender, RoutedEventArgs e)
        {
            bla1.SetSoccer();
        }
    }
}

MainWindow Xaml

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525" xmlns:my="clr-namespace:WpfApplication1">
    <Grid>
        <my:Bla HorizontalAlignment="Left" Margin="0,12,0,0" x:Name="bla1" VerticalAlignment="Top" changeHaus="bla1_changeHaus" changeSoccer="bla1_changeSoccer" />
        <my:Bla HorizontalAlignment="Left" Margin="247,12,0,0" x:Name="bla2" VerticalAlignment="Top" changeHaus="bla2_changeHaus" changeSoccer="bla2_changeSoccer" />
    </Grid>
</Window>