带按钮的 WPF 自定义控件

问题描述 投票:0回答:1

我已经为我的应用程序创建了一个 WPF 自定义控件。我现在正在努力连接控件中某些控件的事件(切换按钮)。

我定义了以下内容:

  • WindowHeader.cs
public class WindowHeader : ContentControl 
{
    public static readonly DependencyProperty TargetSelectedCommandProperty = 
        DependencyProperty.Register(nameof(TargetSelectedCommand), typeof(ICommand), typeof(WindowHeader));   

    public ICommand TargetSelectedCommand
    {
        get => (ICommand)GetValue(TargetSelectedCommandProperty);
        set => SetValue(TargetSelectedCommandProperty, value);
    }}
  • WindowHeader.xaml
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:ctrl="clr-namespace:App.Windows.Lib.CustomControls"
                    x:Class="App.Windows.Lib.Style.CustomControls.WindowHeader"
                    x:ClassModifier="public">

  <Style TargetType="{x:Type ctrl:WindowHeader}">

    <Setter Property="Template">

      <Setter.Value>

        <ControlTemplate TargetType="{x:Type ctrl:WindowHeader}">

          <ctrl:DragArea>

          <Grid>
            <Grid.ColumnDefinitions>
              <ColumnDefinition Width="*" />
              <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

          <TextBlock Grid.Column="0"
                     Style="{DynamicResource TargetSelection_TextBlock}"
                     Text="{TemplateBinding SelectedTargetName}"
                     Visibility="{TemplateBinding ShowTargeting}"/>

            <StackPanel Grid.Column="1"
                        Style="{DynamicResource WindowHeader_StackPanel}">

                <ToggleButton PreviewMouseDown="Target_PreviewMouseDown" 
                              PreviewMouseUp="Target_PreviewMouseUp"
                              Style="{DynamicResource Target_Button}"
                              Visibility="{TemplateBinding ShowTargeting }" />

                <ToggleButton Click="Pin_Click"
                              Style="{DynamicResource Pin_Button}"
                              Visibility="{TemplateBinding ShowPinning}" />

               <ToggleButton Click="Resize_Click"
                             Style="{DynamicResource Resize_Button}"
                             Visibility="{TemplateBinding ShowResizing}"/>
              
            </StackPanel>

          </Grid>

          </ctrl:DragArea>

        </ControlTemplate>
        
      </Setter.Value>
      
    </Setter>
    
  </Style>
  • WindowHeader.xaml.cs
public partial class WindowHeader : ResourceDictionary 
{

    public WindowHeader() 
    { 
        InitializeComponent(); 
    } 
        
    public void Target_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
    -- THIS IS WHERE I WANT KNOWLEDGE OF THE COMMAND PASSED TO WindowHeader.cs
        throw new NotImplementedException();
    }
    
    public void Target_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        throw new NotImplementedException();
    }
}

MainWindow.xaml 中的控件声明:

<ctrl:WindowHeader TargetSelectedCommand="{Binding OutputDestinationViewModel.DestinationSelectedCommand}" />

我想要做的是,当 Target_MouseDown 事件被触发时,它执行通过 WindowHeader.cs 传递的 ICommand TargetSelectedCommand

wpf events custom-controls icommand
1个回答
0
投票
ctrl的ControlTemplate内部:WindowHeader 可以通过使用 TemplateBinding 来限制依赖属性

<ToggleButton Command="{TemplateBinding TargetSelectedCommand}"/>
但是如果您想从资源字典中的处理程序调用 TargetSelectedCommand - 您需要检索 WindowHeader 的实例

public void Target_PreviewMouseDown(object sender, MouseButtonEventArgs e) { var toggleButton = sender as ToggleButton; if (toggleButton is null) return; is (toggleButton.TemplatedParent is WindowHeader control) { control.TargetSelectedCommand?.Execute(); } //throw new NotImplementedException(); }
    
© www.soinside.com 2019 - 2024. All rights reserved.