我对 WPF 比较陌生,有时它让我的头爆炸。但是,我确实喜欢它背后的强大功能,尤其是在与 MVVM 模型一起使用时。
我有一个 ControlTemplate
包含 Button
.我用那个ControlTemplate
在自定义控件中。我想在自定义控件上添加一个属性,该属性将绑定(bind)到 Button
的命令属性在ControlTemplate
里面.基本上,它是一个 ComboBox
用Button
在它的右边允许用户弹出一个搜索对话框。由于此控件可能会多次出现在用户控件上,因此我需要能够将每个控件潜在地绑定(bind)到不同的命令(搜索产品、搜索客户等)。
但是,我一直无法弄清楚该怎么做。
这是一些示例 XAML:
<p>
</p>
<pre><code><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>
</code></pre>
我想我可以通过做这样的事情来让它工作:
<Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type SelectionFieldControl}}, Path=ShowSearchCommand}" Margin="2" />
但它不起作用。
如有任何帮助,我们将不胜感激。
最佳答案
呸!
确实有效。问题是我的依赖属性在注册属性时没有使用 UIPropertyMetadata 作为属性元数据。
公共(public) ICommand ShowSearchCommand
{
得到 { 返回 (ICommand)GetValue(ShowSearchCommandProperty); }
设置 { SetValue(ShowSearchCommandProperty,值); }
}
public static readonly DependencyProperty ShowSearchCommandProperty =
DependencyProperty.Register("ShowSearchCommand", typeof(ICommand),
typeof(SelectionFieldControl),
新的 UIPropertyMetadata(OnShowSearchCommandChanged));
static void OnShowSearchCommandChanged(DependencyObject 发件人,DependencyPropertyChangedEventArgs e)
{
}
关于c# - 在 WPF 中,如何在控件模板中获取命令以绑定(bind)到父项中的属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2876600/