Silverlight模板化按钮控件中的数据绑定到命令

Silverlight模板化按钮控件中的数据绑定到命令,silverlight,data-binding,controltemplate,Silverlight,Data Binding,Controltemplate,我正在尝试创建一个模板化的按钮控件,其中包含可见性、工具提示和命令的数据绑定。可见性绑定可以工作,工具提示也可以,但命令不能。另一个进程负责注入viewmodel并将其与视图关联,其他数据绑定正在工作,因此我非常确信它工作正常 在资源字典中: <Converters:BoolToVisibilityConverter x:Key="boolVisibilityConverter" /> <Style TargetType="local:ImageButton">

我正在尝试创建一个模板化的按钮控件,其中包含可见性、工具提示和命令的数据绑定。可见性绑定可以工作,工具提示也可以,但命令不能。另一个进程负责注入viewmodel并将其与视图关联,其他数据绑定正在工作,因此我非常确信它工作正常

在资源字典中:

<Converters:BoolToVisibilityConverter x:Key="boolVisibilityConverter" />

<Style TargetType="local:ImageButton">
    <Setter Property="Visibility" Value="{Binding FallbackValue=Visible, Path=ToolIsAvailable, Converter={StaticResource boolVisibilityConverter} }"/>
    <Setter Property="Command" Value="{Binding ButtonCommand}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ImageButton">
                <Grid>
                <Image Source="{TemplateBinding Image}" 
                       ToolTipService.ToolTip="{Binding ToolName}"  />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
在视图模型中

public MyCommandViewModel()
        : base("My Tool", true)
    {
    }


    public class CommandViewModel
    {
    public CommandViewModel(string toolName, bool isAvailable)
    {
                    ToolIsAvailable = isAvailable;
                    ToolName = toolName;
        _buttoncommand = new DelegateCommand(() =>
        {
            ExecuteCommand();
        },
        () => { return CanExecute; });
    }

    private bool _canExecute = true;
    public bool CanExecute
    {
        get { return _canExecute; }
        set 
        { 
            _canExecute = value; 
            OnPropertyChanged("CanExecute");  
            if (_command != null) _command.RaiseCanExecuteChanged();  
        }
    }

    private DelegateCommand _buttoncommand;
    public ICommand ButtonCommand
    {
        get { return _buttoncommand; }
    }
    protected virtual void ExecuteCommand()
    {
    }
    public bool ToolIsAvailable
    {
        get { return _toolIsReady; }
        set { _toolIsReady = value; OnPropertyChanged("ToolIsAvailable"); }
    }
    public string ToolName
    {
        get { return _toolName; }
        set { _toolName = value; OnPropertyChanged("ToolName"); }
    }
         }
为什么其他数据绑定功能正常,但命令数据绑定功能不正常。我找到了类似的帖子
我是否需要为网格控件设置模板并使用RoutedCommands?我不知道为什么Silverlight对待命令绑定与其他绑定不同,所以我怀疑代码中有一个bug。

专门寻找datacontext是否有效

Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.ButtonCommand}"

这就是我的解决方案。使用与上述相同的commandviewmodel和相同的MyCommandViewModel

<Style TargetType="local:ImageButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ImageButton">
                <Grid>
                <Image Source="{TemplateBinding Image}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

将代码更改为似乎不会产生任何影响difference@nicoleeschmidt您是否尝试过将CanExecute设置为立即返回true?不,它没有帮助。我更改了_buttoncommand=newdelegateCommand(()=>{ExecuteCommand();},()=>{return true;});实际上,如果我在ButtonCommand的getter上设置了一个断点,但它从未被命中,这说明绑定是错误的,对吗?@nicoleeschmidt如果绑定getter从未被命中,这意味着没有控件请求它。我将使用Silverlight Spy()查看按钮工作的datacontext。
<Style TargetType="local:ImageButton">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="local:ImageButton">
                <Grid>
                <Image Source="{TemplateBinding Image}" />
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
<UserControl x:Class="SilverlightApplication11.Test"
...
   >
    <UserControl.Resources>
        <Converters:BoolToVisibilityConverter x:Key="boolVisibilityConverter" />
    </UserControl.Resources>
    <Grid>
      <local:ImageButton Image="/SilverlightApplication11;component/Themes/hand.png" Command="{Binding ButtonCommand}" Visibility="{Binding FallbackValue=Visible, Path=ToolIsAvailable, Converter={StaticResource boolVisibilityConverter} }"/>
    </Grid>
</UserControl>
    public Test(TestCommandViewModel vm)
    {
        InitializeComponent();

        this.Loaded += (o, e) => this.DataContext = vm;
    }