【问题标题】:wp8 Binding CheckBox to CustomMessageBox via DataTemplatewp8通过DataTemplate将CheckBox绑定到CustomMessageBox
【发布时间】:2015-03-17 04:16:35
【问题描述】:

我自己控制。 在里面我想定义数据模板以在自定义消息框中使用它。 在代码中,我打开此对话框,但无法将起始值设置为其中的复选框。

请帮助我 - 如何通过名为 VoiceTemplate 的 DataTemplate 将 cbVoiceAttChecked 变量正确绑定到 CustomMessageBox

XAML:

<UserControl x:Class="myProj.RDPControl"
...
>

<UserControl.Resources>
    <DataTemplate x:Key="VoiceTemplate" >
        <StackPanel Margin="32,0,0,0">
            <CheckBox x:Name="cbVoiceAtt" Content="..." IsChecked="{Binding cbVoiceAttChecked}"/>
            ... /*Other checkboxes*/
        </StackPanel>
    </DataTemplate>
</UserControl.Resources>

<Grid x:Name="LayoutRoot">
    ... Here is main control - works perfectly
</Grid>

在代码中

public partial class RDPControl : UserControl
{
    public RDPControl()
    {
        InitializeComponent();
        //this.DataContext = this;
    }

    public bool cbVoiceAttChecked { get; set; }

....

    private void VoiceButton_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        cbVoiceAttChecked = true; // This value binding to temlate!!!
        CustomMessageBox messageBox = new CustomMessageBox()
        {
            Caption = "...",
            Message = "...",
            ContentTemplate = (DataTemplate)(this.Resources["VoiceTemplate"]), // Use template from xaml
            DataContext = this, // I want to use cbVoiceAttChecked variable to bind to dialog
            LeftButtonContent = "yes",
            RightButtonContent = "no"
        };
        ...
        messageBox.Show(); 
    }

【问题讨论】:

    标签: wpf xaml windows-phone-8 datatemplate


    【解决方案1】:

    您需要将cbVoiceAttChecked 属性更改为DependencyProperty,或者在RDPControl 类中实现INotifyPropertyChanged 接口。

    您可以在 MSDN 上的 INotifyPropertyChanged Interface 中找到有关 INotifyPropertyChanged 接口的更多信息,并在 MSDN 上的 DependencyProperty ClassDependency Properties Overview 页面中找到有关 DependencyPropertys 的更多信息。

    当然,这完全取决于您对 RDPControl 类中的 ContentTemplate 对象所做的事情。由于您没有表明这一点,我无法确认进行上述更改会解决您的问题。

    【讨论】:

    • 我试试 INotifyPropertyChanged - 问题一样!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    相关资源
    最近更新 更多