可視性、ツールチップ、およびコマンド用のデータバインディングでテンプレートボタンコントロールを作成しようとしています。 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が他のコマンドバインディングとは異なるバインドを扱う理由を理解していないので、コードにバグがあります。
後ろコードで行われ、上記と同様commandviewmodelと同じMyCommandViewModelを用いセッタープロパティ= "コマンド"値= "{バインディングRelativeSource = {RelativeSource FindAncestor、AncestorType = UserControl}、Path = DataContext.ButtonCommand}" />はそう思わない差をつける – nicoleeschmidt
@nicoleeschmidt CanExecuteを設定してただちに真を返そうとしましたか? – Josh
いいえ、それは役に立ちません。私は_buttoncommand = newを変更しました。DelegateCommand(()=> { ExecuteCommand(); }、 ()=> {return true;}); – nicoleeschmidt