我正在开发一个WPF应用程序并使用Caliburn.Micro
用于MVVM。我需要在运行时动态地将按钮添加到视图中。
我做了一些研究,我知道我可以使用ItemControl实现这一点。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<ItemsControl>
<Button x:Name="ExecuteMethod1" Content="Execute1" />
<Button x:Name="ExecuteMethod2" Content="Execute2" />
<Button x:Name="ExecuteMethod3" Content="Execute3" />
</ItemsControl>
</Grid>
</Window>
Caliburn.Micro's
约定允许调用与Button的x:Name
属性匹配的方法。在上面的代码中,名为ExecuteMethod1的按钮将在ViewModel中调用方法ExecuteMethod1()。
问题:
当我动态添加按钮时,如何设置按钮的X:Name属性?
我尝试使用Binding
设置它,但WPF不允许绑定X:Name
属性。
还有其他选择吗?任何人都可以帮我提一个建议吗?
答案 0 :(得分:0)
我不确定这是否是一项新功能。从Caliburn Micro文档中获取了它的信息。 添加Windows交互程序集。您可以将按钮的名称或内容传递给您的操作,该操作应足以识别单击了哪个按钮并执行正确的操作。 CaliburnMicro Actions
<UserControl x:Class="Caliburn.Micro.HelloParameters.ShellView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cal="http://www.caliburnproject.org">
<StackPanel>
<TextBox x:Name="Name" />
<Button Content="Click Me">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<cal:ActionMessage MethodName="SayHello">
<cal:Parameter Value="{Binding ElementName=Name, Path=Text}" />
</cal:ActionMessage>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
</UserControl>