我对WPF比较陌生,有时会让我的头脑爆炸。但是,我确实喜欢它背后的力量,特别是当与MVVM模型一起使用时。
我的ControlTemplate
包含Button
。我在自定义控件中使用ControlTemplate
。我想在自定义控件上添加一个属性,该属性将绑定到Button
内ControlTemplate
的命令属性。基本上,它是ComboBox
,右侧有Button
,允许用户弹出搜索对话框。由于此控件可能多次出现在用户控件上,因此我需要能够将每个控件绑定到不同的命令(搜索产品,搜索客户等)。
但是,我一直无法弄清楚如何做到这一点。
以下是一些示例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>
<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>
我以为我可以通过做这样的事情来实现它:
但它不起作用。
非常感谢任何帮助。
答案 0 :(得分:1)
卫生署!
这确实有效。问题是我的依赖属性在注册属性时没有使用UIPropertyMetadata作为属性Meta数据。
public ICommand ShowSearchCommand
{
get { return (ICommand)GetValue(ShowSearchCommandProperty); }
set { SetValue(ShowSearchCommandProperty, value); }
}
public static readonly DependencyProperty ShowSearchCommandProperty =
DependencyProperty.Register("ShowSearchCommand", typeof(ICommand),
typeof(SelectionFieldControl),
new UIPropertyMetadata(OnShowSearchCommandChanged));
static void OnShowSearchCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}
public static readonly DependencyProperty ShowSearchCommandProperty =
DependencyProperty.Register("ShowSearchCommand", typeof(ICommand),
typeof(SelectionFieldControl),
new UIPropertyMetadata(OnShowSearchCommandChanged));
static void OnShowSearchCommandChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
}