我希望能够做这样的事情:
.xaml.cs:
Error description:
init:do_boot/3 line 823
init:start_em/1 line 1115
rabbit:start_it/1 line 480
rabbit:broker_start/0 line 356
rabbit:start_apps/2 line 575
app_utils:manage_applications/6 line 126
lists:foldl/3 line 1263
rabbit:'-handle_app_error/1-fun-0-'/3 line 696
throw:{could_not_start,rabbitmq_management,
{rabbitmq_management,
{bad_return,
{{rabbit_mgmt_app,start,[normal,[]]},
{'EXIT',
{{could_not_start_listener,
[{port,15672}],
{shutdown,
{failed_to_start_child,ranch_acceptors_sup,
{listen_error,rabbit_web_dispatch_sup_15672,eaddrinuse}}}},
...
.xaml:
public partial class MyControl : UserControl
{
public MyControl() => InitializeComponent();
public static readonly DependencyProperty MyTemplateProperty = DependencyProperty.Register(
"MyTemplate", typeof(DataTemplate), typeof(MyControl), new PropertyMetadata(default(DataTemplate)));
public DataTemplate MyTemplate
{
get => (DataTemplate) GetValue(MyTemplateProperty);
set => SetValue(MyTemplateProperty, value);
}
}
但是它不起作用。并不令人惊讶的是,当您使用<UserControl x:Class="MyControl"> <!-- etc. -->
<Grid />
<!-- does not compile-->
<UserControl.MyTemplate>
<DataTemplate />
</UserControl.MyTemplate>
</UserControl>
开头元素名称时,编译器仅查找在UserControl
本身上定义的属性。但是将元素名称更改为UserControl
(使用正确的名称空间前缀)也不起作用;在这种情况下,编译器会尝试将<MyControl.MyTemplate>
解释为附加属性。
除了在资源中定义值然后从代码隐藏中将其分配给属性外,还有什么方法可以实现?
答案 0 :(得分:2)
您可以通过样式设置属性:
<UserControl ...>
<UserControl.Style>
<Style>
<Setter Property="local:MyControl.MyTemplate">
<Setter.Value>
<DataTemplate />
</Setter.Value>
</Setter>
</Style>
</UserControl.Style>
...
</UserControl>