2

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

在此处输入图像描述

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) {
        }
    }
}

现在的问题是你得到这个视图:

在此处输入图像描述

是否可以触发一次主页按钮并在两个用户控件“BLA”上显示 Img?它们显然是相同的 UserControl。

谢谢你的帮助。

4

1 回答 1

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));

    }
}

用户控件 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();
        }
    }
}

主窗口 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>
于 2012-06-10T18:58:20.867 回答