2016-11-28 11 views
2

rollnumbernameを含む2つのテキストボックスがあり、ListBoxに含まれています。クリックするたびにテキストボックス内にデータが追加されるボタンがあります。ボタンでテキストボックスの背景色を更新する方法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文字列変数に背景色をバインドし、ボタンをクリックして背景色を変更するだけです。 'string color =" Green "' – FakeCaleb

+0

ボタンにはClickプロパティが含まれていないので、そのスタイル内のバインディングは効果を持ちません。それとは別に、テキストボックスの背景を変更する場合は、スタイルをテキストボックスに割り当て、ボタンは割り当てないようにしてください。上記と同様に、あなたのビューモデル内にプロパティを持ち、それをテキストボックスから直接バインドします。そのプロパティはあなたのリストの一部ではないので、ここではrelativesourceバインディングを使う必要があります。 – adminSoftDK

答えて

2

コメント者と同じように、私はViewModelにそのロジックを入れます。ここに例があります。私はGalaSoft.MvvmLightナゲットパッケージを使用しています。

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

のViewModel:

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

答えをありがとう。あなたがここで親戚の縛りを使う必要があった理由を説明してくれれば嬉しいですね。スタイルやトリガだけで達成することは可能ですか?私は、ボタンのクリックがテキストボックスのスタイル変更をトリガーすることを意味しますか?あなたは何を言っていますか ? – Sss

+0

ようこそ。 – PhysXCoder

+0

階層レベル(疑似コード)にする必要があるため、RelativeBindingが必要です。 - Window.DataContext(およびWindow.Grid.DataContext)がRollsViewModelに設定されています。 - Window.ListBox.ItemsSourceがRollsViewModelに設定されています。 emp - Window.ListBox.Item [x] DataTemplateのテキストボックスは、RollsViewModel.emp [x] .RollNoまたは.Nameプロパティにバインドします。 ただし、TextBoxesがバインドするContainsItemsプロパティは、RollsViewModel自体のメンバです。 Gird(RelativeSourceはタイプグリッドのFindAncestor)に移動することで、GridのDataContext、つまりRollViewModelにバインドすることができます。 – PhysXCoder

関連する問題