2016-11-28 69 views
2

我有两个文本框,其中包含rollnumbername并包含在ListBox中。我有按钮,每次点击都会在文本框中添加数据。如何更新文本框backcolor的按钮单击wpf

我想实现的是当我点击按钮时,它必须将两个文本框的背景颜色更改为绿色。 (记住每次点击这个按钮我有一个新的行,在文本框中添加一些文本)。

我试过使用触发器,但还没能做到。代码如下:

<Window.Resources> 
     <Style x:Key="buttonColorChange" TargetType="{x:Type Button}"> 
      <Style.Triggers> 
       <DataTrigger Binding="{Binding Click, ElementName=btnClick}" Value="true"> 
        <Setter Property="Background" Value="Green"></Setter> 
       </DataTrigger>     
      </Style.Triggers> 
     </Style> 
    </Window.Resources> 
    <Grid> 

     <ListBox Name="empLB" ItemsSource="{Binding Path=emp}" Height="100" Width="300" VerticalAlignment="Top"> 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <DockPanel Width="300" >       
         <TextBox Name="txt2" Text="{Binding Path= RollNo}"></TextBox> 
         <TextBox Name="txt1" Text="{Binding Path=Name}"></TextBox> 
        </DockPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate>    
     </ListBox> 
     <Button Style="{StaticResource buttonColorChange}" Name="btnClick" Height="20" Width="100" Content="click On me" Command="{Binding BtnClick}" ></Button> 
    </Grid> 

如何更改按钮文本框的颜色点击绿色的吗?

+2

的EAS y方式只是将背景颜色绑定到字符串变量,并在点击按钮'string color =“Green”时更改它' – FakeCaleb

+0

Button不包含Click属性,因此您在该样式中的绑定不会产生任何效果。除此之外,如果要更改文本框的背景,应将样式分配给文本框,而不是按钮。就像上面说的,在你的视图模型中有一个属性,并直接从你的文本框绑定到它。因为该属性不会成为你的列表的一部分,你需要在这里使用relativesource绑定。 – adminSoftDK

回答

2

和评论者一样,我也将这个逻辑放入ViewModel。这是一个例子。我正在使用GalaSoft.MvvmLight nuget包。

查看XAML:

<Window.Resources> 
    <local:BoolToBrushConverter x:Key="boolToColorConv" /> 
</Window.Resources> 
<Grid> 
    <ListBox Name="empLB" ItemsSource="{Binding Path=emp}" Height="100" Width="300" VerticalAlignment="Top"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <DockPanel Width="300" > 
        <TextBox Name="txt2" 
           Text="{Binding Path= RollNo}" 
           Background="{Binding Path=DataContext.ContainsItems, 
                Converter={StaticResource boolToColorConv}, 
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}} }" /> 
        <TextBox Name="txt1" 
           Text="{Binding Path=Name}" 
           Background="{Binding Path=DataContext.ContainsItems, 
                Converter={StaticResource boolToColorConv}, 
                RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}} }" /> 
       </DockPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
    <Button Name="btnClick" Height="20" Width="100" Content="click On me" Command="{Binding BtnClick}" /> 
</Grid> 

查看代码:

public partial class RollWindow : Window 
{ 
    public RollWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     // You might want to replace this with a ViewModel locator 
     DataContext = new RollsViewModel(); 
    } 
} 

视图模型:

public class RollsViewModel : ViewModelBase 
{ 
    public ObservableCollection<Item> emp 
    { 
     get; 
     set; 
    } 

    public bool ContainsItems 
    { 
     get { return _containsItems; } 
     set { _containsItems = value; RaisePropertyChanged(); } 
    } 
    private bool _containsItems; 

    public RollsViewModel() 
    { 
     emp = new ObservableCollection<Item>(); 
    } 

    public ICommand BtnClick 
    { 
     get 
     {    
      if (_btnClick == null) 
      { 
       _btnClick = new RelayCommand(() => 
       { 
        // Dummy action, replace with call to model 
        emp.Add(new Item() { Name = "A roll", RollNo = emp.Count }); 
        ContainsItems = emp.Count > 0; 
       }); 
      } 
      return _btnClick; 
     } 
    } 
    private RelayCommand _btnClick;  
} 

public class Item : ViewModelBase 
{ 
    public int RollNo 
    { 
     get { return _rollNo; } 
     set { _rollNo = value; RaisePropertyChanged(); } 
    } 
    private int _rollNo; 

    public string Name 
    { 
     get { return _name; } 
     set { _name = value; RaisePropertyChanged(); } 
    } 
    private string _name; 
} 

转换器:

public class BoolToBrushConverter : IValueConverter 
{ 
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     var color = (value is bool && (bool)value) ? System.Windows.Media.Colors.Green : System.Windows.SystemColors.ControlColor; 
     return new SolidColorBrush(color); 
    } 

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) 
    { 
     throw new NotImplementedException(); 
    } 
} 
+0

感谢您的回答。如果你能解释为什么我们需要在这里使用relativesource绑定,这将是非常棒的。是否有可能通过风格和触发来实现?我的意思是按钮单击会触发文本框的样式更改?你说什么 ? – Sss

+0

不客气。 – PhysXCoder

+0

RelativeBinding是必要的,因为我们需要在层次结构级别(伪代码): - Window.DataContext(和Window.Grid.DataContext)设置为RollsViewModel - Window.ListBox.ItemsSource设置为RollsViewModel。 emp - DataTemplate中的Window.ListBox.Item [x] .TextBoxes绑定到RollsViewModel.emp [x] .RollNo或.Name属性 但TextBoxes应绑定到的ContainsItems属性是RollsViewModel本身的成员。通过上传到Gird(RelativeSource是具有Type Grid的FindAncestor),可以绑定到Grid的DataContext,即RollViewModel。 – PhysXCoder

相关问题