Изменение изображения элемента панели инструментов при нажатии - PullRequest
1 голос
/ 18 марта 2011

У меня есть панель инструментов, которая связывает ее ItemsSource с моей виртуальной машиной, это список ToolBarItem

public class ToolbarItem : ObservableObject
{
    public string ToolTip { get; set; }
    public ICommand Command { get; set; }
    public BitmapImage Icon { get; set; }
    private bool _isPressed;

    public bool IsPressed
    {
        get { return _isPressed; }
        set { _isPressed = value; RaisePropertyChanged("IsPressed"); }
    }


    public ToolbarItem(string tooltip, ICommand command, BitmapImage icon)
    {
        this.Icon = icon;
        this.Command = command;
        this.ToolTip = tooltip;            
    }
}

}

это мой шаблон элемента панели инструментов:

<DataTemplate x:Key="toolBarItemTemplate">
    <Button x:Name="toolBarButton"
            ToolTip="{Binding Path=ToolTip}"                
            Command="{Binding Path=Command}"
            Cursor="Hand"
            Style="{StaticResource toolBarButtonItemStyle}">
        <ContentControl Content="{Binding}" ContentTemplate="{StaticResource toolBarButtonItemTemplate}" />
    </Button>
</DataTemplate>

<DataTemplate x:Key="toolBarButtonItemTemplate">
    <Image Width="16"
           Height="16"                 
           Source="{Binding Path=Icon}"
           Style="{StaticResource toolBarButtonImageStyle}" />
</DataTemplate>

То, что я хочу сделать, - это когда пользователь нажимает на конкретную кнопку панели инструментов, чтобы изменить поведение этой кнопки, например, иметь рамку вокруг нее.

Ответы [ 2 ]

0 голосов
/ 19 марта 2011

способ сделать это - использовать ItemTemplateSelector и в зависимости от типа элемента использовать другой DataTemplate.

это делается в коде

0 голосов
/ 18 марта 2011

Если вы не хотите использовать его в качестве кнопки ToggleButton, попробуйте что-то вроде этого:

<DataTemplate x:Key="toolBarItemTemplate"> 
    <Button x:Name="toolBarButton" 
            ToolTip="{Binding Path=ToolTip}"                 
            Command="{Binding Path=Command}" 
            Cursor="Hand" 
            Style="{StaticResource toolBarButtonItemStyle}">
        <!-- added code starts-->
        <Button.Style> 
          <Style TargetType="{x:Type Button}"> 
          <Style.Triggers> 
            <Trigger Property="IsPressed" Value="True">
              <Setter Property="BorderBrush" Value="#FF0000"/>
              <Setter Property="Foreground" Value="#00FF00"/>
            </Trigger>       
          </Style.Triggers> 
          </Style> 
        </Button.Style>
        <!-- added code ends-->
        <ContentControl Content="{Binding}"
            ContentTemplate="{StaticResource toolBarButtonItemTemplate}" />
    </Button> 
</DataTemplate> 

<DataTemplate x:Key="toolBarButtonItemTemplate"> 
    <Image Width="16" 
           Height="16"                  
           Source="{Binding Path=Icon}" 
           Style="{StaticResource toolBarButtonImageStyle}" /> 
</DataTemplate> 
...