2009-08-24 63 views
0

我有一个Windows窗体中的ListView,我绑定的对象列表创建窗体。我想要做的是在按钮点击循环中创建的项目,并将其IsEnabled属性更改为false。我尝试过两种方法,但都不是特别成功。任何人都可以帮助解决这些问题和/或提出一种替代方法吗?通过WPF循环ListView DateTemplate项目

我的ListView的XAML

<ListView Margin="6" Name="myListView" ItemsSource="{Binding Path=.}"> 
    <ListView.ItemTemplate> 
     <DataTemplate> 
     <Grid> 
      <Grid.ColumnDefinitions> 
       <ColumnDefinition Width="10"/> 
       <ColumnDefinition Width="350"/> 
       <ColumnDefinition Width="20"/> 
       <ColumnDefinition Width="350"/> 
      </Grid.ColumnDefinitions> 
      <Grid.RowDefinitions> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
       <RowDefinition Height="30" /> 
      </Grid.RowDefinitions> 
      <TextBlock Name="ItemNameTextBlock" Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" VerticalAlignment="Center" Text="{Binding Path=ItemName}" /> 
      <CheckBox Name="Action1CheckBox" Grid.Row="1" Grid.Column="1" Content="Action1" IsChecked="True" /> 
      <CheckBox Name="Action2CheckBox" Grid.Row="1" Grid.Column="3" Content="Action2" IsChecked="True" /> 
      <TextBox Height="23" Name="MyInputTextBox" Grid.Row="2" Grid.Column="1" Margin="2,0,2,0" VerticalAlignment="Top" Width="25" Text="{Binding Path=DataValue}" />           
     </Grid> 
     </DataTemplate> 
    </ListView.ItemTemplate> 
</ListView> 

目标:按下按钮(一个不相关的按钮)禁用复选框和文本框

尝试1: 这并没有工作,项目是数据绑定项目和我无法找到一种方法来控制自己做这样的事情。这甚至有可能吗?

foreach (var item in ReleaseDeployProcessListView.Items) 
{ 
    ((CheckBox)item.FindControl("Action1CheckBox")).IsEnabled = false; 
} 

尝试2: 我添加了一个公共财产“IsFormElementsEnabled”的形式和按钮点击这个值设置为false。但我无法弄清楚如何/如果/我需要做什么来将它绑定到物品上。我尝试过IsEnabled =“{Binding Path = IsFormElementsEnabled}(它不能工作,因为它绑定到对象,并且不是那些obects的派对),我尝试了IsEnabled =”{Binding Path = this.IsFormElementsEnabled}看起来也不行)

回答

2

你总是可以在你的ViewModel上添加一个布尔值并将它绑定到你的CheckBox上?

所以,想象一下以下布尔你的视图模型:

public bool CanEdit 
{ 
    get 
    { 
     return canEdit; 
    } 
    set 
    { 
     canEdit = value; 
     NotifyPropertyChanged("CanEdit"); 
    } 
} 

public event PropertyChangedEventHandler PropertyChanged; 
private void NotifyPropertyChanged(string info) 
{ 
    if (this.PropertyChanged != null) 
    { 
     this.PropertyChanged(this, new PropertyChangedEventArgs(info)); 
    } 
} 

另外请注意,您的视图模型必须实现INotifyPropertyChanged接口

那么这个布尔的复选框绑定在你的DataTemplate

<CheckBox Name="Action1CheckBox" Grid.Row="1" Grid.Column="1" Content="Action1" IsChecked="True" IsEnabled="{Binding CanEdit}" /> 

并在您的for循环中设置布尔值为CanEdit为false:

foreach (var item in ReleaseDeployProcessListView.Items) 
{ 
    item.CanEdit = false; 
} 
+0

是的,这肯定会工作,我真的很希望我能得到另外两个中的一个机制虽然工作。 – ChrisHDog 2009-08-24 09:29:35

+1

看到我的新答案;)虽然我仍然喜欢这种方法:) – Arcturus 2009-08-24 11:06:11

2

确定这里是如何使双方的解决方案的工作;)

尝试之一:

foreach (var item in ReleaseDeployProcessListView.Items) 
{ 
    ListViewItem i = (ListViewItem) ReleaseDeployProcessListView.ItemContainerGenerator.ContainerFromItem(item); 

    //Seek out the ContentPresenter that actually presents our DataTemplate 
    ContentPresenter contentPresenter = FindVisualChild<ContentPresenter>(i); 

    CheckBox checkbox = (CheckBox)i.ContentTemplate.FindName("Action1CheckBox", contentPresenter); 
    checkbox.IsEnabled = false; 
} 


private T FindVisualChild<T>(DependencyObject obj) 
    where T : DependencyObject 
{ 
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
    { 
     DependencyObject child = VisualTreeHelper.GetChild(obj, i); 
     if (child != null && child is T) 
      return (T)child; 
    } 

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) 
    { 
     DependencyObject child = VisualTreeHelper.GetChild(obj, i); 
     T childOfChild = FindVisualChild<T>(child); 
     if (childOfChild != null) 
      return childOfChild; 
    } 

    return null; 
} 

尝试二:

在您的主要控制

依赖属性:

public bool IsFormElementsEnabled 
{ 
    get { return (bool)GetValue(IsFormElementsEnabledProperty); } 
    set { SetValue(IsFormElementsEnabledProperty, value); } 
} 

public static readonly DependencyProperty IsFormElementsEnabledProperty = 
    DependencyProperty.Register("IsFormElementsEnabled", typeof(bool), typeof(YourClass), new PropertyMetadata(true)); 

然后使用的RelativeSource绑定到你的主类中的CheckBox控件:

<CheckBox Name="Action1CheckBox" Grid.Row="1" Grid.Column="1" Content="Action1" IsChecked="True" 
      IsEnabled="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:YourControl}}, Path=IsFormElementsEnabled}" /> 

正如你可以看到许多条条大路通罗马;)我宁愿虽然使用第一个选项,因为它可能是部分你的业务逻辑,例如决定,如果客户已经支付或类似的东西一个布尔值:CustomerHasPaid

希望这有助于

+0

非常感谢大角星,完美的回答我的问题。我同意其他答案可能更好(我已经标记为正确),但我很好奇这些其他两种方法如何实现。再次感谢! – ChrisHDog 2009-08-24 13:32:18

+0

没问题..很高兴我可以帮忙;) – Arcturus 2009-08-24 13:39:18