【问题标题】:wpf list view row disable button enablewpf 列表视图行禁用按钮启用
【发布时间】:2019-01-07 00:47:11
【问题描述】:

我正在尝试禁用用户对列表视图中行的选择,但我仍然希望该行上的任何按钮都是可点击的。到目前为止,我在 listview 上使用 eventSetter 将事件 previewLeftMouseDown 设置为一个除了以下内容之外什么都不做的函数:

e.handled = true;

但这也使我在该行中的一个按钮无法点击。是否有一个选项可以覆盖事件 previewLeftMouseDown 以便按钮可以点击,或者以不同的方式执行此操作?

我仍然希望通过代码能够突出显示某些行,就好像用户选择了它们一样。

谢谢!

【问题讨论】:

  • 在每个 ListViewitem 中都有 Button ??
  • 是的,每一行都有一个

标签: wpf listview


【解决方案1】:

我用INotifyPropertyChanged 创建了一个名为Data 的类。

public class Data : INotifyPropertyChanged
    {
        public string Text { get; set; }

        private string selectedBackGround;
        public string SelectedBackGround
        {
            get
            {
                return selectedBackGround;
            }
            set
            {
                selectedBackGround = value;
                NotifyPropertyChanged("SelectedBackGround");
            }

        }


        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(String propertyName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

在 Xaml 中,我重写了 ControlTemplate 并使用 SelectedBackGround 属性绑定到 StackPanel 背景。 SelectedBackGround 属性仅用于通过代码更改颜色。

        <ListView x:Name="ListView1" SelectionChanged="ListView_SelectionChanged" HorizontalAlignment="Left" Height="135.924" Margin="194.529,104.462,0,0" VerticalAlignment="Top" Width="302.311" ItemsSource="{Binding ListOfstring}"    >
        <ListView.ItemContainerStyle>
            <Style TargetType="{x:Type ListViewItem}">
                <Style.Setters>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate>
                                <StackPanel Orientation="Horizontal" Background="{Binding SelectedBackGround}">
                                    <TextBlock Text="{Binding Text,UpdateSourceTrigger=Explicit}"  Foreground="Black"/>
                                    <Button  x:Name="btn1" Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="Btn1_Click_1"  />
                                </StackPanel>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style.Setters>
            </Style>
        </ListView.ItemContainerStyle>
    </ListView >

这就是我填写我的收藏的方式。

public MainWindow()
    {
        ListOfstring = new ObservableCollection<Data>();
        InitializeComponent();
        ListOfstring.Add( new Data{ Text="TEST1", SelectedBackGround =    "White"       });
        ListOfstring.Add( new Data{ Text="TEST2", SelectedBackGround =    "White"   });
        ListOfstring.Add( new Data{ Text="TEST3", SelectedBackGround =    "White"   });
        ListOfstring.Add( new Data{ Text = "TEST4", SelectedBackGround  = "White" });
        this.DataContext = this;

    }

如您所见,Button 是通过点击事件订阅的。

private void Btn1_Click_1(object sender, RoutedEventArgs e)
{
    // Whenever you click on the any list item button, you are changing background 
    // of the 3rd item in the list view to Aqua Color.
    ListOfstring[2].SelectedBackGround = "Aqua";
}

【讨论】:

    猜你喜欢
    • 2015-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 2021-05-18
    相关资源
    最近更新 更多