2016-11-28 64 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

相關問題