如何從ListBox風格的DataTrigger中更改按鈕的IsEnabled屬性?我嘗試了很多選擇,而且都一樣,我沒有解決方案。我怎麼能改變ListBox風格的DataTrigger的按鈕的IsEnabled屬性?
<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 x:Name="lbStudents" Grid.Column="0" AlternationCount="2" BorderThickness="0 0 1 1" BorderBrush="black" Margin="0 0 0 5" ItemsSource="{Binding Students}"
SelectedItem="{Binding SelectedStudent}" SelectionMode="Single">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Name="stpListBoxItem" Margin="4 7 4 7">
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat=" {0} {1}">
<Binding Path="LastName"/>
<Binding Path="FirstName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button x:Name="btnSave" Command="{Binding SaveCommand}" Grid.Column="2">
<Image Source="Resources/icons/save-icon.png"></Image>
<Button.Style>
<Style TargetType="Button">
<Style.Triggers>
<DataTrigger Binding="{Binding Items.Count, ElementName=lbStudents}" Value="0">
<Setter Property="IsEnabled" Value="True" />
</DataTrigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
我花了很多時間來這一點,但仍然沒有找到解決辦法的。對不起,我的英語不好。
爲你的Button.Style創建一個'Trigger',然後使用'Binding.ElementName'屬性這樣的回答https://stackoverflow.com/a/34129469/2946329 –
ListBox Style中的Setter不能設置Button的屬性。 XAML中的ListBox在哪裏? – mm8