【问题标题】:Wpf binding Observablecollection and RelayCommand [duplicate]Wpf绑定Observablecollection和RelayCommand [重复]
【发布时间】:2019-07-31 08:41:37
【问题描述】:

我在将 RelayCommand 绑定到数据网格中的按钮时遇到问题,该按钮已将 ItemSource 绑定到 ObservableCollection。

我的问题是 RelayCommand (RemoveCommand) 不可见,无法从 viewmodel 执行。

<Button Grid.Column="0" Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}" 
                    ToolTip="Remove" 
                    Command="{Binding Path=RemoveCommand}">
                        <materialDesign:PackIcon Kind="Close" Height="24" Width="24" />
                    </Button>

只有可见的东西是类中的方法,如 IconName 等,它们来自以前的 ItemSource 绑定

<WindowsControl:DataGrid ItemsSource="{Binding ListOfExcelFiles ,IsAsync=True,Mode=TwoWay}"

但我想使用 viewmodel 中的 RelayCommand 可以以某种方式执行此操作,或者我需要重写我的代码?

视图模型:

private ObservableCollection<ExcelFile> _listOfExcelFiles;
public ObservableCollection<ExcelFile> ListOfExcelFiles
{
get { return _listOfExcelFiles; }
set
{
    _listOfExcelFiles = value;
    OnPropertyChanged();
}
}

型号:

 public class ExcelFile :  INotifyPropertyChanged 
    {
        string _name;
        string _filePath;
        string _iconName = "File";
        string _validationIcon = "";
        bool _showbutton = false;

        public bool ShowButton
        {
            get { return _showbutton; }
            set
            {
                _showbutton = value;
                OnPropertyChanged();
            }
        }
        public string IconName
        {
            get { return _iconName; }
            set
            {
                _iconName = value;
            }
        }
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
            }
        }
        public string FilePath
        {
            get { return _filePath; }
            set
            {
                _filePath = value;
            }
        }       

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

我的 XAML:

    <WindowsControl:DataGrid ItemsSource="{Binding ListOfExcelFiles 
    ,IsAsync=True,Mode=TwoWay}" 
    AutoGenerateColumns="False" 
    Grid.Column="0" Grid.Row="0" 
    SelectionMode="Single" 
    SelectionUnit="FullRow" 
    CanUserAddRows="false" 
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="Icon" CellStyle="{StaticResource CenteredCell}">
            <DataGridTemplateColumn.CellTemplate >
                <DataTemplate >
                    <materialDesign:PackIcon Kind="{Binding IconName}" 
                    Height="24" Width="24" />
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="FileName"  
        Width="*" IsReadOnly="True" 
        CellStyle="{StaticResource CenteredLeftCell}" >
            <DataGridTextColumn.Binding>
                <Binding Path="Name" 
                NotifyOnTargetUpdated="True" 
                UpdateSourceTrigger="PropertyChanged" 
                Mode="TwoWay">
                </Binding>
            </DataGridTextColumn.Binding>
        </DataGridTextColumn>
        <DataGridTextColumn Header="FilePath" 
        Binding="{Binding FilePath}" 
        Width="*" IsReadOnly="True" 
        CellStyle="{StaticResource CenteredLeftCell}"></DataGridTextColumn>
        <DataGridTemplateColumn CellStyle="{StaticResource CenteredCell}" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Grid Visibility="{Binding ShowButton,Converter={StaticResource BoolToVisibility},FallbackValue=Hidden}">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                            <ColumnDefinition Width="*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <Button Grid.Column="0" Style="{StaticResource MaterialDesignFloatingActionMiniDarkButton}" 
                        ToolTip="Remove" 
                        Command="{Binding Path=RemoveCommand}">
                            <materialDesign:PackIcon Kind="Close" Height="24" Width="24" />
                        </Button>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</WindowsControl:DataGrid>

【问题讨论】:

  • 范围错误。当前范围是DataTemplate。试试Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=DataGrid}, Path=DataContext.RemoveCommand }" /&gt;

标签: c# wpf xaml mvvm


【解决方案1】:

在您的 Binding 中使用带有 FindAncestor 的 RelativeSource 并引用您的 DataGrid(...您的 ViewModel)的 DataContext

Command={Binding Path=DataContext.RemoveCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}

RelativeSource 导致绑定在应用提供的路径之前搜索提供的类型的祖先。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-23
    • 2021-04-29
    • 1970-01-01
    • 2015-01-26
    • 2013-10-14
    • 2015-04-02
    • 2013-04-18
    • 1970-01-01
    相关资源
    最近更新 更多