且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将字符串参数传递给文本块

更新时间:2021-12-13 22:41:10

您可以将一个对象设置为 ContentDialog.DataContext,然后使用绑定来实现您想要的.

You can have an object set as the ContentDialog.DataContext and then use binding to achieve what you want.

在您的 Button.Click 处理程序中,设置数据上下文:

In your Button.Click handler, set the data context:

private void Button_Click(object sender, RoutedEventArgs args)
{
    ContentDialog dialog = new ContentDialog
    {
        DataContext = new
        {
            RedText = "Red Colour",
            BlueText = "Blue Colour"
        }
    };

    dialog.ShowAsync();
}

然后在 ContentDialog 的 XAML 中,您可以具有以下内容:

Then in the XAML of the ContentDialog, you can have something as:

<ContentDialog>
    <TextBlock>Hey <TextBlock Background="Red" Text="{Binding RedText}"/>, well <TextBlock Background="Blue" Text="{Binding BlueText}"/></TextBlock>
</ContentDialog>