0

我只想为我正在使用的 WPF 编辑器启用或禁用 ControlTemplate 中的按钮。

4

4 回答 4

4

我建议通过 RoutedUICommands 轻松控制按钮 - 包括轻松启用和禁用它们。命令不需要对 Button 的任何类型的引用,因此这也解决了您的问题。

看这里:

<Window.CommandBindings>
    <CommandBinding 
        Command="{x:Static local:MyCommands.MyCommand}"
        Executed="CommandBinding_Executed"
        CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>

<Window.Resources>
    <ControlTemplate x:Key="MyTemplate" TargetType="Label">
        <Button Command="{x:Static local:MyCommands.MyCommand}">
            <TextBlock>
                Click Me
                <TextBlock Text="{TemplateBinding Content}" />
            </TextBlock>
        </Button>
    </ControlTemplate>
</Window.Resources>

<StackPanel>
    <Label Template="{StaticResource MyTemplate}">1</Label>
    <Label Template="{StaticResource MyTemplate}">2</Label>
    <Label Template="{StaticResource MyTemplate}">3</Label>
    <Label Template="{StaticResource MyTemplate}">4</Label>
    <Label Template="{StaticResource MyTemplate}">5</Label>
</StackPanel>

用这个:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void CommandBinding_CanExecute(object sender, System.Windows.Input.CanExecuteRoutedEventArgs e)
    {
        // your logic here
        int _Integer = -1;
        int.TryParse(e.Parameter.ToString(), out _Integer);
        e.CanExecute = _Integer % 2 == 0;
    }

    private void CommandBinding_Executed(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
    {
        // do something when clicked
    }
}

public static class MyCommands
{
    public static RoutedUICommand MyCommand = new RoutedUICommand();
}

好像:

截屏

于 2011-08-10T19:49:03.333 回答
3

你真的不应该明确地这样做,而应该使用 TriggersControlTemplate本身来设置它。这些触发器将对其他一些更改做出反应,并将设置Enabled.Button

于 2008-10-08T21:14:44.680 回答
2

我同意 Joel 的观点,首选方法是使用 xaml 标记触发按钮的 Enabled 属性的设置,通过触发器或通过将 Enabled 值绑定到可能在父元素上的另一个依赖项属性,可能借助 a值转换器。

但是,如果您必须在 C# 中显式执行此操作,则可以在按钮的模板属性上使用 FindName() 方法。这里有一个 MSDN 'how to'链接

于 2008-10-08T23:06:36.637 回答
0

我对 controlTemplates 和 dataTemplates 中的按钮有疑问。我发现触发器通常过于繁琐,无法准确地提供我的客户想要的东西,我最终得到了太多的转换器。我第一次尝试的方法是内联定义这些更难的模板,以便它们可以绑定到我的屏幕 ViewModel 上的命令(和代码隐藏事件)。

如果您正在模板化自己的控件,我应该提到最简单的方法是简单地命名按钮,然后使用如下内容:

hourHand   = this.Template.FindName( "PART_HourHand",   this ) as Rectangle;

然而,最近,我已经改变了技巧(回到单独的文件......重用!)但开始添加一个 ViewModel(各种各样的),我将它作为后缀...TemplateCommandStore添加到我的所有模板中,这些模板变得有点复杂。我什至在模板化我的自定义控件时使用它(主要是为了保持一致性)

public class BookingDetailsTemplateCommandStore
{
    public OpenWindowCommand_Command OpenWindowCommand_Command { get; set; }

    public BookingDetailsTemplateCommandStore()
    {
        OpenWindowCommand_Command = new OpenWindowCommand_Command( this );
    }       
}

然后我可以愉快地在模板中使用这个命令。您还可以通过将(依赖关系?)属性绑定到命令存储来将控件引用传递到命令存储中,以捕获事件并对模板进行更精确的控制。

<DataTemplate DataType="{x:Type LeisureServices:BookingSummary}">
    <Border Height="50" otherStyling="xyz">
        <Border.Resources>
            <local:BookingDetailsTemplateCommandStore x:Key="CommandStore" />
        </Border.Resources>
        <DockPanel>
            <Button otherStyling="xyz"
                    Command="{Binding Source={StaticResource CommandStore},
                                      Path=OpenWindowCommand_Command}" />

MVVM:我发现只要图形逻辑就是这样,并且与业务逻辑完全不同,这种方法就不会干扰 MVVM。本质上,我为模板添加了一致的 C# 代码能力,任何在 winforms 上花费太多时间的人都可能会像我一样错过它。

于 2012-03-23T13:28:53.777 回答