设置CustomMessageBox的DataContext

3

我需要在 xaml 中设置自定义消息框的 DataTemplate,就像这样:

<Grid.Resources>
    <DataTemplate x:Key="CustomMessageBoxTemplate">
        <phone:LongListSelector
            LayoutMode="Grid"
            ItemsSource="{Binding}"
            GridCellSize="210 200"
            toolkit:TiltEffect.IsTiltEnabled="True"
            ItemTemplate="{StaticResource ItemTemplate}" />
    </DataTemplate>
</Grid.Resources>

我正在尝试在代码后端中使用它,如下:

CustomMessageBox messageBox = new CustomMessageBox()
{
    Caption = "Title",
    DataContext = myList,
    ContentTemplate = (DataTemplate)this.LayoutRoot.Resources["CustomMessageBoxTemplate"],
    LeftButtonContent = "OK"
};
messageBox.Show();

在我的代码中,myList 已经正确设置为 List(通过调试进行了确认)。

但问题是,我的DataContext没有发生变化,导致我在 CustomMessageBox 中的 LongListSelector 无法填充数据。

我不知道自己哪里出错了。我已经尝试搜索如何解决此问题,但没有找到有用的信息。

感谢任何帮助!


在设置DataContext之前,您应该尝试设置ContentTemplate - Kevin Gosse
1
我已经尝试过了,但是没有任何改变。 - Szymon Bartnik
1
messageBox.Show() 之前,DataContext 仍然为 null - Szymon Bartnik
1个回答

0

ContentTemplate 中的 XAML 的 DataContext 会自动设置为 Content 属性。尝试设置 Content = myList


我不能给你点赞,因为我的声望太低了,但非常非常感谢!我将你的答案标记为最佳。 - Szymon Bartnik

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接