2012-04-18 2 views
0

可視性、ツールチップ、およびコマンド用のデータバインディングでテンプレートボタンコントロールを作成しようとしています。 Visibilityバインディングはツールヒントと同様に機能しますが、コマンドは機能しません。別のプロセスは、ビューモデルを注入し、それをビューに関連付ける責任があり、他のデータバインディングが機能しているため、正常に動作していると確信しています。リソースディクショナリでSilverlightでコマンドへのデータバインディングテンプレートボタンコントロール

<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 class MyButton: ImageButton 
{ 
    public MyButton(MyCommandViewModel viewmodel) 
    { 
     this.DefaultStyleKey = typeof(ImageButton); 
     this.Image = new BitmapImage(new Uri("/MyProject;component/Themes/myimage.png", UriKind.Relative)); 
       this.DataContext = viewmodel; 
    } 

} 

とビューモデルで

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"); } 
    } 
     } 

他のデータバインディングが適切ではなく、コマンドデータを機能しているのはなぜ結合。私はこの類似のポストを見つけた Overriding a templated Button's Command in WPF 代わりにグリッドコントロールをテンプレートし、RoutedCommandsを使う必要がありますか?私は、Silverlightが他のコマンドバインディングとは異なるバインドを扱う理由を理解していないので、コードにバグがあります。

答えて

0

具体的にdatacontext作業を探していますか?

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

後ろコードで行われ、上記と同様commandviewmodelと同じMyCommandViewModelを用いセッタープロパティ= "コマンド"値= "{バインディングRelativeSource = {RelativeSource FindAncestor、AncestorType = UserControl}、Path = DataContext.ButtonCommand}" />はそう思わない差をつける – nicoleeschmidt

+0

@nicoleeschmidt CanExecuteを設定してただちに真を返そうとしましたか? – Josh

+0

いいえ、それは役に立ちません。私は_buttoncommand = newを変更しました。DelegateCommand(()=> { ExecuteCommand(); }、 ()=> {return true;}); – nicoleeschmidt

0

これは私のソリューションでした。

<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> 

と\t \t <へコードを変更

public Test(TestCommandViewModel vm) 
    { 
     InitializeComponent(); 

     this.Loaded += (o, e) => this.DataContext = vm; 
    } 
関連する問題