none
如何在控件之间调用属性? RRS feed

  • 问题

  • hi,大家好。WPF开发时,能否在控件A中的事件去调用控件B呢?比如我在窗口中同时存在两个控件,这两个控件里各有一个按钮,现在我点击控件A的按钮1,控件A隐藏的同时控件B显示。同理当按下控件B的按钮时,控件B隐藏并显示控件A。(显示控件B,A的方法中并不生成新的控件,而是显示已隐藏的)
    2018年9月14日 18:28

答案

  • @CZAupup,

    Yes, if you use usercontrol, in fact, the practice is the same.

    Firstly, I creste two usercontrol, one is usercontrol3, another is usercontrol4, there are one button and one TextBlock in two usercontrol, set Button FieldModifier is public.

    <StackPanel>
            <Button
                Name="btn3"
                Width="200"
                Height="30"
                x:FieldModifier="public"
                Content="btn3" />
            <TextBlock
                Width="200"
                Height="30"
                Margin="0,10,0,10"
                FontSize="15"
                Text="this is usercontrol3" />
        </StackPanel>
    <StackPanel>
            <Button
                Name="btn4"
                Width="200"
                Height="30"
                x:FieldModifier="public"
                Content="btn4" />
            <TextBlock
                Width="200"
                Height="30"
                Margin="0,10,0,10"
                FontSize="15"
                Text="this is usercontrol4" />
    
        </StackPanel>

    Then you can use two usercontrol in Mainwindow, and detect button in code behind.

     <usercontrol:UserControl3 Width="300" Height="70" x:Name="usercontrol3" ></usercontrol:UserControl3>
            <usercontrol:UserControl4 Width="300" Height="70" x:Name="usercontrol4"></usercontrol:UserControl4>
     public partial class Window24 : Window
        {
         
            public Button btn1 { get; set; }
            public Button btn2 { get; set; }
            public Window24()
            {
                InitializeComponent();
               
                btn1 = usercontrol3.btn3;
                btn1.Click += Btn1_Click;
                btn2 = usercontrol4.btn4;
                btn2.Click += Btn2_Click;
                    
            }
    
            private void Btn2_Click(object sender, RoutedEventArgs e)
            {
                usercontrol4.Visibility = Visibility.Collapsed;
                usercontrol3.Visibility = Visibility.Visible;
            }
    
            private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                usercontrol3.Visibility = Visibility.Collapsed;
                usercontrol4.Visibility = Visibility.Visible;
            }
    
           
        }
    

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 CZAupup 2018年9月18日 14:02
    2018年9月18日 9:15
    版主

全部回复

  • Hi CZAupup,

    根据你的描述,你说想在A控件的事件中调用B控件的属性, 这是可以的。 你说A, B控件中各有一个按钮, 那我可以理解为A,B控件实际上是一种容器控件吗? 如果是的话, 假如您使用的A,B容器控件是GroupBox, 里面各自先放如一个Button按钮,在A控件的按钮事件中影藏A控件,显示B控件, 在B控件的按钮事件中隐藏B控件。显示A控件, 如下所示:

     <StackPanel>
            <GroupBox Header="test1" Name="group1">
                <StackPanel>
                    <Button
                        Name="btn1"
                        Width="200"
                        Height="30"
                        Margin="0,10,10,10"
                        Click="btn1_Click"
                        Content="btn1" />
                    <Button
                        Name="btn2"
                        Width="200"
                        Height="30"
                        Margin="0,10,10,10"
                        Content="btn2" />
                </StackPanel>
            </GroupBox>
    
            <GroupBox Header="test2" Name="group2">
                <StackPanel>
                    <Button
                        Name="btn3"
                        Width="200"
                        Height="30"
                        Margin="0,10,10,10"
                        Click="btn3_Click"
                        Content="btn3" />
                    <Button
                        Name="btn4"
                        Width="200"
                        Height="30"
                        Margin="0,10,10,10"
                        Content="btn4" />
                </StackPanel>
            </GroupBox>
        </StackPanel>
     public partial class Window24 : Window
        {
            public Window24()
            {
                InitializeComponent();
            }
    
            private void btn1_Click(object sender, RoutedEventArgs e)
            {
                group1.Visibility = Visibility.Collapsed;
                group2.Visibility = Visibility.Visible;
            }
    
            private void btn3_Click(object sender, RoutedEventArgs e)
            {
                group2.Visibility = Visibility.Collapsed;
                group1.Visibility = Visibility.Visible;
            }
        }

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2018年9月17日 3:19
    版主
  • Hi CZAupup,

    根据你的描述,你说想在A控件的事件中调用B控件的属性, 这是可以的。 你说A, B控件中各有一个按钮, 那我可以理解为A,B控件实际上是一种容器控件吗?

    额,不是这个意思,我表达错了。我是想在两个用户控件(USERCONTROL)之间控制对方的属性,我可以贴个根布局的XAML出来:

        <Grid x:Name="grid1">
            <StackPanel Orientation="Horizontal">
                <local:UserControl1 x:Name="uc1" HorizontalAlignment="Left" Height="100" Margin="10,27,0,0" VerticalAlignment="Top" Width="100"/>
                <local:UserControl2 x:Name="uc2" HorizontalAlignment="Left" Height="100" Margin="235,27,0,0" VerticalAlignment="Top" Width="100" Visibility="Hidden"/>
            </StackPanel>
        </Grid>

    用过UC1的按钮来控制UC2的可见属性。我现在能想到的方法就是绑定UC2的可见属性到UC1的BUTTON上,这样做可以嘛?


    2018年9月17日 14:19
  • @CZAupup,

    Yes, if you use usercontrol, in fact, the practice is the same.

    Firstly, I creste two usercontrol, one is usercontrol3, another is usercontrol4, there are one button and one TextBlock in two usercontrol, set Button FieldModifier is public.

    <StackPanel>
            <Button
                Name="btn3"
                Width="200"
                Height="30"
                x:FieldModifier="public"
                Content="btn3" />
            <TextBlock
                Width="200"
                Height="30"
                Margin="0,10,0,10"
                FontSize="15"
                Text="this is usercontrol3" />
        </StackPanel>
    <StackPanel>
            <Button
                Name="btn4"
                Width="200"
                Height="30"
                x:FieldModifier="public"
                Content="btn4" />
            <TextBlock
                Width="200"
                Height="30"
                Margin="0,10,0,10"
                FontSize="15"
                Text="this is usercontrol4" />
    
        </StackPanel>

    Then you can use two usercontrol in Mainwindow, and detect button in code behind.

     <usercontrol:UserControl3 Width="300" Height="70" x:Name="usercontrol3" ></usercontrol:UserControl3>
            <usercontrol:UserControl4 Width="300" Height="70" x:Name="usercontrol4"></usercontrol:UserControl4>
     public partial class Window24 : Window
        {
         
            public Button btn1 { get; set; }
            public Button btn2 { get; set; }
            public Window24()
            {
                InitializeComponent();
               
                btn1 = usercontrol3.btn3;
                btn1.Click += Btn1_Click;
                btn2 = usercontrol4.btn4;
                btn2.Click += Btn2_Click;
                    
            }
    
            private void Btn2_Click(object sender, RoutedEventArgs e)
            {
                usercontrol4.Visibility = Visibility.Collapsed;
                usercontrol3.Visibility = Visibility.Visible;
            }
    
            private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                usercontrol3.Visibility = Visibility.Collapsed;
                usercontrol4.Visibility = Visibility.Visible;
            }
    
           
        }
    

    Best Regards,

    Cherry



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 CZAupup 2018年9月18日 14:02
    2018年9月18日 9:15
    版主