C# 如何更改按钮';ListBox样式上的DataTrigger的IsEnabled属性?
如何从ListBox样式上的DataTrigger更改按钮的IsEnabled属性?我尝试了很多选择,但仍然没有解决办法C# 如何更改按钮';ListBox样式上的DataTrigger的IsEnabled属性?,c#,wpf,xaml,listbox,C#,Wpf,Xaml,Listbox,如何从ListBox样式上的DataTrigger更改按钮的IsEnabled属性?我尝试了很多选择,但仍然没有解决办法 <Style TargetType="ListBox"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" Val
<Style TargetType="ListBox">
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=Items.Count}" Value="0">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Border BorderThickness="0 0 0.55 0.55" BorderBrush="black" Margin="0 0 0 5">
<TextBlock Foreground="Black" Text="Нет студентов" Margin="20" FontSize="11"></TextBlock>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
</Style.Triggers>
</Style>
<Grid x:Name="gridButtons" Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.1*" />
<ColumnDefinition Width="0.1*" />
<ColumnDefinition Width="0.1*" />
<ColumnDefinition Width="0.2*" />
</Grid.ColumnDefinitions>
<Button Command="{Binding AddCommand}" Grid.Column="0">
<Image Source="Resources/icons/add-icon.png"></Image>
</Button>
<Button Command="{Binding RemoveCommand}" CommandParameter="{Binding SelectedStudent}" Grid.Column="1">
<Image Source="Resources/icons/delete-icon.png"></Image>
</Button>
<Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
<Image Source="Resources/icons/save-icon.png"></Image>
</Button>
<Button Command="{Binding RefreshCommand}" Grid.Column="3">
<Image Source="Resources/icons/refresh-icon.png"></Image>
</Button>
</Grid>
我需要当ListBox项计数为0时,btnSave的属性IsEnabled为True
我按照mm8给我写的那样做了,但仍然不起作用
我做了什么
如何从ListBox样式上的DataTrigger更改按钮的IsEnabled属性
你不能。列表框
样式
中的设置器
只能设置应用样式
的列表框
的属性
您可以将样式
应用于绑定到列表框的属性的按钮
,不过:
<Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
<Image Source="Resources/icons/save-icon.png"></Image>
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, ElementName=listBox}" Value="0">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
仅当带有“ListBox”的x:Name
的列表框
与按钮
在同一命名范围内时,此选项才有效
当列表框的ItemsSource
属性绑定到的源集合计数为0
时,您真正应该做的是从SaveCommand的CanExecute
方法返回false
如何从ListBox样式上的DataTrigger更改按钮的IsEnabled属性
你不能。列表框
样式
中的设置器
只能设置应用样式
的列表框
的属性
您可以将样式
应用于绑定到列表框的属性的按钮
,不过:
<Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
<Image Source="Resources/icons/save-icon.png"></Image>
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, ElementName=listBox}" Value="0">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
仅当带有“ListBox”的x:Name
的列表框
与按钮
在同一命名范围内时,此选项才有效
当列表框的ItemsSource
属性绑定到的源集合计数为0
时,您真正应该做的是从SaveCommand
的CanExecute
方法返回false
,为按钮创建触发器。Style
和然后像这样使用Binding.ElementName
属性回答ListBox样式的Setter不能设置按钮的属性。XAML中的列表框在哪里?为按钮创建一个触发器。Style
,然后使用绑定。ElementName
属性类似于此回答列表框样式中的Setter无法设置按钮的属性。您的XAML中的列表框在哪里?对不起,这不起作用,我不明白为什么。我照你写的做了。你忘了
。只需复制我的示例样式,并将“ElementName=listBox”替换为“ElementName=lbStudents”。并且请不要发布不是您问题答案的答案。改为编辑问题。“您真正应该做的是从SaveCommand的CanExecute方法返回false…”这帮助我找出了问题所在。。。我的CanExecute方法一直在返回true,这干扰了我的样式尝试,但这不起作用,我不明白为什么。我照你写的做了。你忘了
。只需复制我的示例样式,并将“ElementName=listBox”替换为“ElementName=lbStudents”。并且请不要发布不是您问题答案的答案。改为编辑问题。“您真正应该做的是从SaveCommand的CanExecute方法返回false…”这帮助我找出了问题所在。。。我的CanExecute方法一直在返回true,这干扰了我的造型尝试