如何在wpf中点击按钮更新文本框背景色

时间:2016-11-28 12:47:49

标签: c# .net wpf mvvm

我有两个文本框,其中包含rollnumbername,并包含在ListBox中。我有按钮,每次点击都会在textBoxes中添加数据。

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

我尝试使用触发器,但还没能实现。代码如下:

 <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>

如何将按钮上的文本框颜色更改为绿色?

1 个答案:

答案 0 :(得分: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();
    }
}