绑定UserControl和MainWindow [英] Binding UserControl and MainWindow

查看:93
本文介绍了绑定UserControl和MainWindow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!告诉我如何进行绑定:例如 - 在主窗口中单击ToggleButton以更改CheckBox属性IsChecked in UserControl



Hi! Tell me how to make a binding:for example - at click in the main window ToggleButton to change CheckBox property IsChecked in UserControl

<Window x:Class="WpfApplication2.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">
    <Grid>
        <ToggleButton Width="100" Height="60" Content="togglebutton" IsChecked="True"></ToggleButton>
    </Grid>
         <local:UserControl1 HorizontalAlignment="Left" Margin="233,256,0,0" VerticalAlignment="Top"/>
</Window>










<UserControl x:Class="WpfApplication2.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" 

             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <CheckBox IsChecked="True" Width="100" Content="checkbox" Margin="100,125,100,150"/>
    </Grid>
</UserControl>

推荐答案

问题是:通常用户控件的子控件是私有的或根本没有成员声明(如果添加名称属性),因此它们不会直接显示给拥有用户控件实例的表单或其他容器。



那就是说,你需要使用它将用户控件的某些接口暴露给代码。使用复选框状态,它很简单:

The problem is: normally the children of a user controls are private or don''t have member declarations at all (the declaration will appear if you add a Name attribute in XAML), so they are not directly visible to the form or other container owning an instance of your user control.

That said, you need to expose some interface of the user control to the code using it. With the state of check box, it''s simple:
partial class MyUserControl { 

    CheckBox myCheckBox; // will appear if in XAML you add the attribute Name="myCheckBox", it will be private by default
    // actually, the declaration will appear in the auto-generated part of the partial declaration

    // ...

    public bool? IsMyCheckBoxChecked { 
        get { return myCheckBox.IsChecked; }
        set { myCheckBox.IsChecked = value; }
    }
    // not that the property type is nit bool but bool?: the third bool? value, null,
    // will be used if myCheckBox.IsThreeState property is set to true

}





请参阅:http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton .ischecked.aspx [ ^ ]。



您没有询问有关揭露事件的信息,但我会告诉您这个想法。



如果你想要揭露事件需要更多思考,例如 Checked 事件:

http://msdn.microsoft.com/en -us /库/ system.windows.controls .primitives.buttonbase.click.aspx [ ^ ]。



您需要在 MyUserControl中声明一个事件(让我们称之为 MyUserControl.MyCheckBoxCheckedChanged ),然后将一个事件处理程序添加到调用列表 myCheckBox.Checked 。此事件处理程序应调用新创建的事件实例 MyUserControl.MyCheckBoxCheckedChanged ,并且,例如,将复选框的选中状态传递给事件参数。当您在窗口代码中添加 MyUserControl.MyCheckBoxCheckedChanged 的事件处理程序时,处理程序将在单击后接收带有复选框状态信息的事件参数。



完成后,您将能够以与可用FCL控件完全相同的方式绑定用户控件的相应属性或事件。



-SA



Please see: http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.togglebutton.ischecked.aspx[^].

You did not ask about exposing the events, but I will give you the idea.

If will require a bit more thinking if you want to expose the event, such as Checked event:
http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.buttonbase.click.aspx[^].

You will need to declare an event in MyUserControl (let''s call it MyUserControl.MyCheckBoxCheckedChanged), then add an event handler to the invocation list myCheckBox.Checked. This event handler should invoke your newly created event instance MyUserControl.MyCheckBoxCheckedChanged and, say, pass the checked state of the check box to the event arguments. When you add an event handler of MyUserControl.MyCheckBoxCheckedChanged in the window code, the handler will receive event arguments with the information on the check box state after the click.

When this is done, you will be able to bind corresponding properties or events of your user control in exact same ways as with available FCL controls.

—SA


假设他们不共享相同的视图模型,MainWindow包含UserControl1s的'ViewModel,你仍然可以做它使用DelegateCommand http://wpftutorial.net/DelegateCommand.html

还有其他解决方案...... 清洁方式取​​决于背景。
Assuming they don''t share the same view model nor MainWindow contains UserControl1s'' ViewModel, you can still do it with DelegateCommand http://wpftutorial.net/DelegateCommand.html
there is others solutions… the "cleaner" way depends on context.


这篇关于绑定UserControl和MainWindow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆