【问题标题】:Enabling button based on ListView checkbox checked/unchecked WPF启用基于 ListView 复选框选中/未选中 WPF 的按钮
【发布时间】:2017-01-28 11:05:28
【问题描述】:

我这里有个情况。

我有一个带有复选框的 ListView。复选框被定义为数据模板。 我有另一个堆栈面板,它包含一个按钮。我的要求是,最初当窗口加载时,没有选中任何复选框并且应该禁用按钮。 当用户单击任何一个复选框时,应启用按钮。 当用户取消选中所有复选框时,应禁用按钮。

我正在使用 MVVM 模式和牛框架。借助事件,我可以实现此功能。

但我的问题是,我们能否在 xaml 本身中处理这种情况。可能正在使用触发器或转换器?

我是 WPF 新手,对 WPF 没有深入的了解。任何帮助表示赞赏。

我尝试过使用触发器。但由于复选框位于 ListView 内并定义为数据模板,我无法从按钮样式访问复选框的元素名称。

大致轮廓如下

<ListView x:Name="MylistView" ItemsSource="{Binding Collection}" Height="250" 
              SelectionMode="Single" 
               BorderBrush="Transparent">
        <ListView.ItemTemplate>
            <DataTemplate>
                <CheckBox  x:Name="Checkbox" Content="{Binding Description}" IsChecked="{Binding IsEnable, Mode=TwoWay}" />
            </DataTemplate>
        </ListView.ItemTemplate>
</ListView>

<StackPanel Orientation="Horizontal">
        <Button x:Name="Save" Content="{l:Language Key=SAVE}" Width="50" Height="25" Margin="10" 
                 Command="{Binding Updpate, Source={StaticResource proxy}}">
<Button.Style>
//How do i refer checkbox from this level. I get only "Mylistview" control    at this level
   </Button.Style>
  </Button>
</StackPanel>

提前致谢

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    您必须声明一个属性 Visibility 并使用 PropertyChanged 事件实现为:

    private bool _Visibility;
    public bool Visibility
     { get 
          {
             return _Visibility ; 
          }
         set
          {
              _Visibility  = value;
               RaisePropertyChanged("Visibility");
          }
      }
    
    internal void RaisePropertyChanged(string prop)
     {
         if (PropertyChanged != null)
          { 
            PropertyChanged(this, new PropertyChangedEventArgs(prop)); 
          }
     }
    
    
    public event PropertyChangedEventHandler PropertyChanged;
    

    设计

    <Button Name="btnsdf"  Width="35" Height="35" Style="{DynamicResource MetroCircleButtonStyle}" ToolTip="Add Denomination"  >
         <Image Width="30" Source="/Images/icons/add.png"  Margin="0,1,0,0"></Image>
         <Button.Resources>
              <Style TargetType="Button">
                   <Style.Triggers>
                        <DataTrigger Binding="{Binding Visibility,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="true">
                             Setter Property="Visibility" Value="Visible"></Setter>
                         </DataTrigger>
                         <DataTrigger Binding="{Binding Visibility,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Value="false">
                             <Setter Property="Visibility" Value="Collapsed"></Setter>
                         </DataTrigger>
                    </Style.Triggers>
                </Style>
           </Button.Resources>
      </Button>
    

    您需要在 Checkbox Checked 和 unchecked 事件上设置 IsEnabled。我希望这会给你一个想法。

    【讨论】:

    • 谢谢!我可以这样做,但由于某些实现,IsChecked 绑定到“模型”而不是 ViewModel 中定义的属性。列表视图的数据源在 ViewModel 中定义。按钮 IsEnabled 绑定在视图模型中。所以目前我正在使用事件在复选框选中/取消选中时从模型到视图模型进行通信。这符合我的目的。但是除了事件通知,有什么方法可以在 xaml 和 viewmodel 中处理吗?如果模型中没有定义“IsChecked”,这个答案就可以满足我的要求
    • 您也可以在模型中实现 RaisePropertyMethod。无需在 ViewModel 中声明。
    • 这里 DataSource 在 ViewModel 中,而 IsChecked 属性在 Model 中定义。所以必须有一些从 Model(IsChecked) 到 viewModel(DataSource) 的通信来启用和禁用按钮,因为它取决于 Listview 中的项目数。而不是不能我们只是在 xaml 中做到这一点?就像,有什么方法可以通过从 xaml 本身读取 listview 复选框项目来定义触发器以启用和禁用?或者可以通过使用转换器。
    • 但是您可以在视图模型中获取 IsChecked 属性的值。我认为应该没有问题。
    • 是的。目前 IsChecked 绑定到模型,我不应该更改它。如果它绑定到 viewModel 就不会有问题。当复选框选中\取消选中时,有没有办法让我在 viewModel 中获得更改通知。
    猜你喜欢
    • 1970-01-01
    • 2015-08-02
    • 2017-12-28
    • 2018-11-07
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    相关资源
    最近更新 更多