gpt4 book ai didi

c# - 如何在wpf中单击按钮更新文本框背景色

转载 作者:行者123 更新时间:2023-11-30 23:18:32 27 4
gpt4 key购买 nike

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

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

我尝试使用触发器,但还不能成功。代码如下:

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

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

最佳答案

像评论者一样,我也会将该逻辑放入 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();
}
}

View 模型:

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();
}
}

关于c# - 如何在wpf中单击按钮更新文本框背景色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40844740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com