2017-09-22 39 views
0

如何從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>

我花了很多時間來這一點,但仍然沒有找到解決辦法的。對不起,我的英語不好。

+0

爲你的Button.Style創建一個'Trigger',然後使用'Binding.ElementName'屬性這樣的回答https://stackoverflow.com/a/34129469/2946329 –

+0

ListBox Style中的Setter不能設置Button的屬性。 XAML中的ListBox在哪裏? – mm8

回答

1

如何從ListBox風格的DataTrigger更改按鈕的IsEnabled屬性?

你不行。 A SetterListBoxStyle只能設置應用StyleListBox的屬性。

你可以申請一個Style的是結合了ListBox的屬性雖然Button

<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是相同的命名範圍作爲Button

你真正應該做的是從你的SaveCommandCanExecute方法返回false時的ListBoxItemsSource屬性綁定到源集合具有0計數。

+0

對不起,但這不起作用,我不明白爲什麼。當你寫信給我時,我做了所有事情。 –

+0

你忘了''。只需複製我的示例樣式,並用「ElementName = lbStudents」替換「ElementName = listBox」。 – mm8

+0

請不要發佈不是您的問題的答案。改爲編輯問題。 – mm8

相關問題