如何从ResourceDictionary中的控件公开事件

时间:2018-10-26 07:50:48

标签: wpf user-controls resourcedictionary

我正在设计一个UserControl,其中引用了ResourceDictionaryUserControl有一个<ItemsControl>,其中的ItemTemplateResourceDictionary中定义。
简而言之,此<ItemsControl.ItemsSource>接受字符串的集合,并且DataTemplate为每个字符串分配一个<Button>。这是一个以字符串集合为源的动态按钮集合。

UserControl的{​​{1}}声明:

ItemsControl

<ItemsControl Name="PanelMain" Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="1" ItemsSource="{Binding Path=MenuMain}" ItemTemplate="{StaticResource MenuItemTemplate}"> </ItemsControl> 模板:

ResourceDictionary

现在,我需要将每个按钮的点击挂到命令上。由于这些按钮是在<DataTemplate x:Key="MenuItemTemplate" DataType="ItemsControl"> <Button Style="{StaticResource MenuItemStyle}" Content="{Binding}" > </Button> </DataTemplate> <Style x:Key="MenuItemStyle" TargetType="Button"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="Button"> <Border Background="{TemplateBinding Background}" BorderThickness="0" Height="30"> <ContentControl Foreground="{TemplateBinding Foreground}" VerticalAlignment="Center"> <ContentControl.Content> <TemplateBinding Property="Content"/> </ContentControl.Content> </ContentControl> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> 中定义的,因此我不确定如何绑定其命令。一些答案指出了为ResourceDictionary创建代码隐藏文件(.xaml.cs),但是我想知道是否有可能使用键将命令公开为资源,因此其ResourceDictionary可以将其绑定而不是字典吗?

旁注:UserControl必须将其单击的按钮标题显示为字符串

0 个答案:

没有答案
相关问题