如何为Windows::UI::XAML::Controls中的Checkbox添加标签
为WinUI 3 CheckBox添加标签(C++/WinRT)
在WinUI 3的C++/WinRT编程中,Checkbox的标签通过Content属性设置,有两种常见实现方式:
1. 直接设置文本标签
直接使用box_value()将字符串包装为WinRT要求的IInspectable类型,赋值给Content属性:
StackPanel st; CheckBox checkBox; checkBox.Name(L"myCheckBox"); // 修正原代码的Name设置方式,WinRT属性通过方法传参设置 // 设置文本标签 checkBox.Content(box_value(L"同意用户协议")); st.Children().Append(checkBox);
2. 设置复杂内容作为标签
如果需要带样式或嵌套元素的标签,可以创建TextBlock等UI元素作为Content:
StackPanel st; CheckBox checkBox; checkBox.Name(L"styledCheckBox"); TextBlock labelText; labelText.Text(L"启用高级功能"); labelText.FontSize(14); labelText.Foreground(Media::SolidColorBrush(Colors::DarkBlue())); checkBox.Content(labelText); st.Children().Append(checkBox);
注意
原代码中checkBox.Name() = L"";的写法不符合C++/WinRT的属性设置规范,正确的方式是调用Name(L"xxx")方法传入参数。
内容的提问来源于stack exchange,提问作者Tausif
相关产品推荐
相关产品推荐

