2017-08-02 11 views
1

data bindingのプロパティがtrueの場合、buttonbackgroundを変更しようとしています。データバインディング値がtrueの場合、XAML変更ボタンの背景

概要を表示するには、ListBoxがあります。

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

は今、私はこのスタイルにボタンを置きたい:私もこのListBoxにアイテムのスタイルを与えました。だから、私はそうのようなのControlTemplateを使用(私はこれは奇妙に見える知っている、と私は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> 

ので、上記の作品を正しく。 NameおよびAmountは、DataBindingから設定されています。 IsSelectedtrueの場合は、backgroundを設定していません。

私のバインディングオブジェクトは、次のようになります。

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プロパティを変更するには、Binding仕事をするために、このオブジェクトにINotifyPropertyChangedインタフェースを実装する必要がある場合。

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)); 
    } 
} 
関連する問題