我对 WPF 比较陌生,有时它会让我头晕目眩。但是,我确实喜欢它背后的强大功能,尤其是与 MVVM 模型一起使用时。
我有一个ControlTemplate
包含一个Button
. 我ControlTemplate
在自定义控件中使用它。我想在自定义控件上添加一个属性,该属性将绑定Button
到ControlTemplate
. 基本上,它的右侧是一个ComboBox
允许Button
用户弹出搜索对话框的a。由于此控件可能多次出现在用户控件上,因此我需要能够将每个控件潜在地绑定到不同的命令(搜索产品、搜索客户等)。
但是,我一直无法弄清楚如何做到这一点。
这是一些示例 XAML:
<Style TargetType="{x:Type m:SelectionFieldControl}">
<Setter Property="LookupTemplate" Value="{StaticResource LookupTemplate}" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type m:SelectionFieldControl}">
<Border BorderThickness="{TemplateBinding Border.BorderThickness}"
Padding="{TemplateBinding Control.Padding}"
BorderBrush="{TemplateBinding Border.BorderBrush}"
Background="{TemplateBinding Panel.Background}"
SnapsToDevicePixels="True"
Focusable="False">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" MinWidth="0"
SharedSizeGroup="{Binding LabelShareSizeGroupName,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:BaseFieldControl}}}" />
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="Auto"
SharedSizeGroup="{Binding WidgetsShareSizeGroupName,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:BaseFieldControl}}}" />
</Grid.ColumnDefinitions>
<!-- Customized Value Part -->
<ComboBox x:Name="PART_Value"
Grid.Column="1"
Margin="4,2,0,1"
SelectedValue="{Binding Path=SelectionField.Value,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:SelectionFieldControl}}}"
IsEnabled="{Binding Field.IsNotReadOnly,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type m:SelectionFieldControl}}}"
Visibility="{Binding Field.IsInEditMode, Converter={StaticResource TrueToVisible},
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type m:SelectionFieldControl}}}"
FontFamily="{StaticResource FontFamily_Default}" FontSize="11px">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel IsVirtualizing="True"
VirtualizationMode="Recycling"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
<StackPanel Grid.Column="2"
Orientation="Horizontal"
Name="PART_Extra"
Focusable="False">
<ContentControl Name="PART_LookupContent"
Template="{Binding LookupTemplate,
RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type m:SelectionFieldControl}}}"
Focusable="False"/>
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
我想我可以通过做这样的事情来让它工作:
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type SelectionFieldControl}}, Path=ShowSearchCommand}" Margin="2" />
但它不起作用。
任何帮助将不胜感激。