c# - WPF 启用按钮组

标签 c# wpf xaml

在第一列中,我有组列表(“第一”、“第二”)。在第二列中,我有 5 个按钮。在第一列中选​​择按钮组后,我想启用或禁用按钮组。如何在 MVVM 模式中做到这一点?

enter image description here

enter image description here

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        DataContext = new Data();
    }
}
public class Data
{
    public List<string> Items { get; set; }
    public Data()
    {
        Items = new List<string>();
        Items.Add("First");
        Items.Add("Second");
    }
}

Xaml:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ListView ItemsSource="{Binding Items}" Grid.Column="0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding .}"></Label>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Content="First" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="21,24,0,0" VerticalAlignment="Top" Width="76" IsEnabled="False"/>
    <Button Content="Second" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="102,48,0,0" VerticalAlignment="Top" Width="76" IsEnabled="True"/>
    <Button Content="First" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="33,83,0,0" VerticalAlignment="Top" Width="76" IsEnabled="False"/>
    <Button Content="Second" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="126,93,0,0" VerticalAlignment="Top" Width="76" IsEnabled="True" RenderTransformOrigin="1.088,-0.075"/>
    <Button Content="Second" Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="93,186,0,0" VerticalAlignment="Top" Width="76" IsEnabled="True"/>
</Grid>

最佳答案

创建您的 VM 类,如:

public class Data:INotifyPropertyChanged
{
    public ObservableCollection<MyRecord> Items
    {
       get{
            return _items;
       }
       set{
            _items=value;
            OnProperyChanged("Items")
        }
    }

    public Data()
    {
        Items = new ObservableCollection<MyRecord>()
        {
                new MyRecord(){Title:"First",IsEnable:false}),
                new MyRecord(){Title:"Second",IsEnable:false})
        };
    }

    //---------------------------------------------------------------
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnProperyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            var handler= PropertyChanged ;
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

public class MyRecord:INotifyPropertyChanged
{
    private int _title;
    private bool _isEnable;

    public int Title
    {
        get
        {
            return _title;
        }
        set
        {
            _title= value;
            OnProperyChanged("Title")
        }
    }

    public bool IsEnable
    {
        get
        {
            return _isEnable;
        }
        set
        {
            _isEnable= value;
            OnProperyChanged("IsEnable")
        }
    }

    //---------------------------------------------------------------
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnProperyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            var handler= PropertyChanged ;
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

现在在您的 Xaml 中将每个 ButtonIsEnabled 属性绑定(bind)到适当的属性...

你可以用BooleanToVisibilityConverter写出下面的代码:

//First add BooleanToVisibilityConverter to the window resources


// When you know Items[0] is FIRST use some code like this>>
IsEnabled="{Binding Items[0].IsEnable, converter={StaticResource BooleanToVisibilityConverter}}" 

示例:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition></ColumnDefinition>
        <ColumnDefinition></ColumnDefinition>
    </Grid.ColumnDefinitions>
    <ListView ItemsSource="{Binding Items}" Grid.Column="0">
        <ListView.ItemTemplate>
            <DataTemplate>
                <Label Content="{Binding .}"></Label>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button Content="First"
            IsEnabled="{Binding Items[0].IsEnable, converter={StaticResource BooleanToVisibilityConverter}}"
            Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="21,24,0,0" VerticalAlignment="Top" Width="76"/>
    <Button Content="Second"
            IsEnabled="{Binding Items[1].IsEnable, converter={StaticResource BooleanToVisibilityConverter}}"
            Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="102,48,0,0" VerticalAlignment="Top" Width="76"/>
    <Button Content="First"
            IsEnabled="{Binding Items[0].IsEnable, converter={StaticResource BooleanToVisibilityConverter}}"
            Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="33,83,0,0" VerticalAlignment="Top" Width="76"/>
    <Button Content="Second"
            IsEnabled="{Binding Items[1].IsEnable, converter={StaticResource BooleanToVisibilityConverter}}"
            Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="126,93,0,0" VerticalAlignment="Top" Width="76" RenderTransformOrigin="1.088,-0.075"/>
    <Button Content="Second"
            IsEnabled="{Binding Items[1].IsEnable, converter={StaticResource BooleanToVisibilityConverter}}"
            Grid.Column="1" HorizontalAlignment="Left" Height="30" Margin="93,186,0,0" VerticalAlignment="Top" Width="76"/>
</Grid>

好的,在这一步之前一切都很好。现在,如果您将每个项目的 IsEnable 属性设置为 TRUE,则按钮必须为 Enabled,反之亦然。要执行它,您可以:

  • 在您的数据类中编写一个命令,并在每个列表之间进行绑定(bind) 项和命令。
  • 使用 ListView.SelectionChanged 事件使 Clicked 项的 IsEnable 属性为 TRUE注意:现在,ListView 中每个项目的类型都是 MyRecord

关于c# - WPF 启用按钮组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32012135/

相关文章:

c# - WPF:如何将通用数据绑定(bind)到 TreeView?

c# - XDocument.Save() 在每个 XElement 上添加不需要的命名空间

c# - C# 中的 Moq Application.Current.MainWindow

c# - g.i.cs 文件丢失,类不再包含 InitializeComponent 的定义

c# - 为什么两个进程比 2 个线程有优势?

c# - Ranorex 测试自动化问题 : Unable to reliably click a button on silverlight web app

c# - 复制一个 ObservableCollection 项

c# - 如何以编程方式将视频通话设置为全屏启动? (Lync SDK)

c# - 如何在 Metro 应用程序中打印文本文件或文本框的内容?

c# - WPF: 'ListView.ItemsSource' 属性在 'Loaded' 事件期间为空