【发布时间】:2019-09-12 11:59:03
【问题描述】:
我在将UserControl 中的Button 绑定到父级中的Command 时遇到问题。
我有一个显示搜索结果数据的UserControl (SearchView)。
其中包含一个Menu,我想在其中包含一个重置用户设置(字体大小、排序等)的功能。由于有多个这样的我想在其父控件(MainView)中具有重置功能。
MainViewXaml:
<UserControl x:class="UI.MainView"
xmlns:ui="clr-namespace:Project.Main.UI">
//some stuff here
<ui:SearchView/>
//some stuff here
</UserControl>
MainView.xaml.vb:
Namespace UI
Public Class MainView
Public ReadOnly Property ResetCommand As New DelegateCommand(AddressOf ResetEinstellungen)
Public Sub ResetEinstellungen()
//Reset Einstellungen ...
End Sub
搜索视图:
<UserControl x:Class="UI.SearchView"
xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
<Grid>
//Table Stuff
</Grid>
<Menu>
<MenueItem Header="Reset"
x:Name="ResetButton">
<dxmvvm:Interaction.Behaviors>
<dxmvvm:EventToCommand EventName="Click"
Command="{Binding ResetCommand , RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl} }">
</dxmvvm:EventToCommand>
</dxmvvm:Interaction.Behaviors>
</MenuItem>
</Menu>
我已经尝试了一些变化。
我要避免的是通过 ElementName 引用 MainView,因为我想在其他视图中使用 UserControl。
【问题讨论】: