WPF命令绑定项样式中的控件
我在WPF命令绑定项样式中的控件,wpf,xaml,command,styles,Wpf,Xaml,Command,Styles,我在Textboxstyles.xaml中有一个样式,如下所示 <Style x:Key="EmptyItemsControlUsabilityDashboard2017Style" TargetType="ItemsControl"> <Style.Triggers> <Trigger Property="HasItems" Value="false"> <Setter Prope
Textboxstyles.xaml中有一个样式,如下所示
<Style x:Key="EmptyItemsControlUsabilityDashboard2017Style" TargetType="ItemsControl">
<Style.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Control">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Height="12" Width="12" Source="/YoLo;component/Resources/Images/link.png" Margin="0,3,0,0" />
<TextBlock x:Name="EmptyCollectionTextBox" Text="{x:Static UsabilityDashboard2017Loc:DashboardUsability2017Resource.lblNumNotDefined}"
Style="{StaticResource UsabilityDashboard2017TextBoxStyle}"
HorizontalAlignment="Center"
Margin="5,25,0,25"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<ItemsControl ItemsSource="{Binding YoLoViewModelsCollection}" Name="YoLoViewModelsItemSource" Style="{StaticResource EmptyItemsControlUsabilityDashboard2017Style}">
我在另一个Xaml文件中使用了它,如下所示
<Style x:Key="EmptyItemsControlUsabilityDashboard2017Style" TargetType="ItemsControl">
<Style.Triggers>
<Trigger Property="HasItems" Value="false">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Control">
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Image Height="12" Width="12" Source="/YoLo;component/Resources/Images/link.png" Margin="0,3,0,0" />
<TextBlock x:Name="EmptyCollectionTextBox" Text="{x:Static UsabilityDashboard2017Loc:DashboardUsability2017Resource.lblNumNotDefined}"
Style="{StaticResource UsabilityDashboard2017TextBoxStyle}"
HorizontalAlignment="Center"
Margin="5,25,0,25"/>
</StackPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
<ItemsControl ItemsSource="{Binding YoLoViewModelsCollection}" Name="YoLoViewModelsItemSource" Style="{StaticResource EmptyItemsControlUsabilityDashboard2017Style}">
现在它显示了一个文本框,该集合为空,但是如何在名为“EmptyCollectionTextBox”的文本块上设置命令绑定,在用户单击它执行命令的样式中
我已经看到了自定义命令,但不知何故它们不起作用。这段代码实际上有很多错误。首先,我不知道触发器应该做什么,看起来只有在列表中没有元素时才会设置controltemplate
其次,看起来您正试图用图像和文本表示集合中的每个元素,所有这些都在ItemsControl中。这不是通过模板化整个控件来实现的,而是通过模板化ItemTemplate来实现的。您使用的是DataTemplate,而不是ControlTemplate:
现在回到您的实际问题,您希望在单击TextBlock时收到通知。有很多不同的方法可以做到这一点,但在这种情况下,您最好用按钮替换该文本块,然后用表示为文本块的ControlTemplate覆盖该模板。这使您能够两全其美:从GUI的角度来看,它实际上仍然是一个文本块,但您仍然可以获得所有的按钮单击通知和命令处理程序等:
<Image />
<Button Command="{Binding ClickedCommand}" Cursor="Hand">
<Button.Template>
<ControlTemplate>
<TextBlock Text="Text Binding Goes Here" />
</ControlTemplate>
</Button.Template>
</Button>
此特定示例假定集合中的项具有名为“ClickedCommand”的命令处理程序。实际上,您的处理程序可能位于父类(例如,主窗口的视图模型)中,在这种情况下,您需要为主窗口指定一个x:Name(例如“_this”)并绑定到该名称,将该项作为CommandParameter传入,以便它知道单击了哪个:
<Button Command="{Binding ElementName=_this, Path=DataContext.ClickedCommand}" CommandParameter="{Binding}" Cursor="Hand">
然后,主视图模型有一个处理程序,看起来像这样:
public ICommand ClickedCommand { get { return new RelayCommand<YourCollectionItemType>(OnClicked); } }
private void OnClicked(YourCollectionItemType item)
{
}
public ICommand clicked命令{get{returnnewrelaycommand(OnClicked);}
单击私有void(YourCollectionItemType项)
{
}
也许吧。@dymanoid谢谢你的链接,但不是真的。我需要按样式做,我看不到文本框。你是说文本块吗?TextBlock没有命令属性,因此再次使用;你想做什么?对不起,我不明白你的问题。默认情况下,文本块不可单击,但您可以为其处理MouseLeftButtonEvent。仅当列表中没有元素时,才会设置controltemplate?=>是的,没错。是的,但我想把所有这些都转移到样式中,那么我可以在样式中进行绑定吗?好的,在这种情况下,只需使用
,并将ControlTemplate设置为您的文本块。