2017-08-02 69 views
1

如果data binding的属性为true,那么我试图更改buttonbackground如果数据绑定值为真,则XAML更改按钮背景

给一个简短的概述 - 我有一个ListBox。我也给这个ListBox为项目样式:

<ListBox Margin="0,5,0,0" Background="Transparent" 
      BorderThickness="0" 
      ItemsSource="{Binding PaymentsAndCollectionData.PaymentsToCreditors}" 
      SelectedItem="{Binding PaymentsAndCollectionData.SelectedPaymentToCreditor}"> 
     <ListBox.ItemContainerStyle> 
      ....... 
     </ListBox.ItemContainerStyle> 
    </ListBox 

现在,我想提出一个按钮,在这种风格。所以,我用像这样一个控件模板(我知道这看起来奇怪,我可以propably做不同的!):

<Style TargetType="ListBoxItem"> 
<Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="ListBoxItem"> 
      <Button> 
       <Button.Style> 
        <Style TargetType="Button"> 
         <Setter Property="Template"> 
          <Setter.Value> 
           <ControlTemplate> 
            <Button Name="Button" HorizontalContentAlignment="Stretch" Background="Transparent" BorderThickness="0" 
              Command="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, 
                 Path=DataContext.PaymentsAndCollectionData.SelectPaymentToCreditorCommand}" 
              CommandParameter="{Binding}"> 
             <DockPanel Name="DP" LastChildFill="False" Background="{TemplateBinding Background}"> 
              <TextBlock DockPanel.Dock="Left" Text="{Binding Name}" Margin="20,0,10,0" Padding="0,5,0,5" Foreground="{TemplateBinding Foreground}"/> 
              <StackPanel Orientation="Horizontal" DockPanel.Dock="Right"> 
               <TextBlock Text="R" VerticalAlignment="Center" Foreground="{TemplateBinding Foreground}"/> 
               <TextBlock Text="{Binding Amount, StringFormat=N2}" VerticalAlignment="Center" Margin="0,0,10,0" Foreground="{TemplateBinding Foreground}"/> 
              </StackPanel> 
             </DockPanel> 
            </Button> 
            <ControlTemplate.Triggers> 
             <DataTrigger Binding="{Binding IsSelected}" Value="True"> 
              <Setter TargetName="DP" Property="Background" Value="{StaticResource APPLICATION_GREEN_COLOR}" /> 
              <Setter Property="Foreground" Value="White" /> 
             </DataTrigger> 
            </ControlTemplate.Triggers> 
           </ControlTemplate> 
          </Setter.Value> 
         </Setter> 
        </Style> 
       </Button.Style> 
      </Button> 
     </ControlTemplate> 
    </Setter.Value> 
</Setter> 
</Style> 

因此,上述工作正常。 NameAmountDataBinding开始设置。我只是没有设置background,如果IsSelectedtrue

我的绑定对象是这样的:

public class PaymentItem 
{ 
    public string Name { get; set; } 

    public decimal Amount { get; set; } 

    public bool IsSelected { get; set; } 
} 
+0

的可能的复制[WPF数据绑定不更新?](https://stackoverflow.com/questions/14965796/wpf-databinding-not-updating) – tabby

回答

1

如果你改变了IsSelected属性创建对象后,您需要实现INotifyPropertyChanged接口在此对象,使Binding工作。

public class PaymentItem : INotifyPropertyChanged 
{ 
    private bool _isSelected; 

    public string Name { get; set; } 

    public decimal Amount { get; set; } 

    public bool IsSelected 
    { 
     get 
     { 
      return _isSelected; 
     } 
     set 
     { 
      _isSelected = value; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
}