【问题标题】:Filter on Silverlight Datagrid using Radio Buttons C#使用单选按钮 C# 在 Silverlight Datagrid 上过滤
【发布时间】:2012-01-09 09:49:59
【问题描述】:

我有一个数据网格:

<Grid x:Name="MainDataGrid" Grid.Row="5" >
                    <data:DataGrid x:Name="datagrid" AutoGenerateColumns="False" HorizontalAlignment="Stretch" IsReadOnly="True" VerticalAlignment="Top" ItemsSource="{Binding DataList}" Height="220" Margin="5">
                        <data:DataGrid.Columns>
                            <data:DataGridTextColumn Header="#" Binding="{Binding Number}" />
                            <data:DataGridTextColumn Header="Business Process" Width="300" Binding="{Binding BusinessProcess}" />
                            <data:DataGridTextColumn Header="Sub Process" Width="300" Binding="{Binding SubProcess}"  />
                            <data:DataGridTextColumn Header="ITAC" Width="100" Binding="{Binding Itac}"  />
                            <data:DataGridTextColumn Header="Status" Width="200" Binding="{Binding Status}" />
                        </data:DataGrid.Columns>
                    </data:DataGrid>
                </Grid>

目前绑定到我在 ViewModel 中创建的一些虚拟数据:

 public class ItacViewModel : ViewModelBase
{
    public List<data> DataList { get; set; }

    public ItacViewModel()
    {
        var dataList = new List<data>
                           {
                               new data(1, "Fixed Asset", "Depreciation", "UL-AC1", "Under Review"),
                               new data(2, "Fixed Asset", "Depreciation", "UL-AC2", "Complete"),
                               new data(3, "Order to Cash", "Invoicing", "UL-AC3", "Under Review"),
                               new data(4, "Order to Cash", "Shipping Goods", "UL-AC4", "Under Review"),
                               new data(5, "Order to Cash", "Depreciation", "UL-AC5", "Complete"),
                               new data(6, "Order to Cash", "Depreciation", "UL-AC6", "Under Review"),
                               new data(7, "Order to Cash", "Depreciation", "UL-AC7", "Complete"),
                               new data(8, "Stock and Make", "Depreciation", "UL-AC8", "Under Review"),
                               new data(9, "Stock and Make", "Depreciation", "UL-AC9", "Under Review"),
                               new data(10, "General Accounting & Reporting", "Depreciation", "UL-AC10", "Complete")
                           };
        DataList = dataList;
    }
}

我希望能够在选择单选按钮时过滤数据(单选按钮名称与数据网格中第二列中的名称匹配):

                            <StackPanel Margin="2">
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <RadioButton GroupName="BusinessProcessesGroup" />
                                <TextBlock Text="All Business Processes" FontStyle="Italic" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <RadioButton GroupName="BusinessProcessesGroup" />
                                <TextBlock Text="Fixed Asset" FontStyle="Italic" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <RadioButton GroupName="BusinessProcessesGroup" />
                                <TextBlock Text="Order to Cash" FontStyle="Italic" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <RadioButton GroupName="BusinessProcessesGroup" />
                                <TextBlock Text="Purchase to Pay" FontStyle="Italic" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <RadioButton GroupName="BusinessProcessesGroup" />
                                <TextBlock Text="Stock and Make" FontStyle="Italic" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <RadioButton GroupName="BusinessProcessesGroup" />
                                <TextBlock Text="General Accounting and Reporting" FontStyle="Italic" />
                            </StackPanel>
                            <TextBlock Text=" " />
                        </StackPanel>

到目前为止,我还无法完成这项工作。

编辑 到目前为止,这是我的所有代码: MAINPAGE.XAML

    <UserControl x:Class="TabControl.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" mc:Ignorable="d"
    d:DesignHeight="500" d:DesignWidth="800">

    <Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <controls:TabControl Margin="3">
            <controls:TabItem Header="Local / Monitoring Controls" />
            <controls:TabItem Header="IBM Controls" />
            <controls:TabItem Header="ITAC's" >
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <StackPanel Grid.Row="0">
                        <Border BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" >
                            <StackPanel Orientation="Horizontal" >
                                <TextBlock Text="ITAC's - IT Automated Controls in Key Financial Systems" FontSize="12" FontWeight="Bold" Margin="5" />
                                <StackPanel Orientation="Horizontal" Margin="5">
                                    <TextBlock Text="Date Last Updated: " />
                                    <TextBlock Text="Test Date, needs binding" />
                                </StackPanel>
                            </StackPanel>
                        </Border>
                        <Border BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2">
                            <TextBlock Text="ERP controls from Sirius" FontStyle="Italic" Margin="5" />
                        </Border>
                    </StackPanel>

                    
                    <Grid x:Name="Controls" Grid.Row="1" Background="#A1AEC3">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="*" />
                        </Grid.RowDefinitions>
                        
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>

                        <Border Grid.Row="0" Grid.ColumnSpan="2" BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" Margin="1" >
                            <TextBlock Text="IT Automated Controls (Europe)" FontSize="12" FontWeight="Bold" Margin="5"/>
                        </Border>

                        
                        <StackPanel Grid.Row="1" Grid.Column="0" Margin="0,0,10,0" >
                            <TextBlock Text="Total Active Controls" FontWeight="Bold" Margin="5,5,5,0" TextAlignment="Right" />
                            <TextBlock Text="ITAC's Under Review"  Margin="5,5,5,0" TextAlignment="Right"  />
                            <TextBlock Grid.Row="3" Grid.Column="0" Text="Outstanding Issues from Previous Months"  Margin="5,5,5,0" TextAlignment="Right"  />
                        </StackPanel>


                        <StackPanel Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0" >
                            <TextBox Width="200" TextAlignment="Center" Text="33" />
                            <TextBox Width="200" TextAlignment="Center" Text="5" />
                            <TextBox Width="200" TextAlignment="Center" Text="0" />
                        </StackPanel>


                    </Grid>

                    <Grid x:Name="ITACs" Grid.Row="2" Background="#A1AEC3">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="*" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <Border Grid.Row="0" Grid.Column="0" BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" Margin="1" >
                        <StackPanel Orientation="Horizontal" Margin="3">
                            <StackPanel Orientation="Horizontal" >
                                <RadioButton GroupName="BusinessProcessTitle" />
                                <TextBlock Text="All ITAC's" FontWeight="Bold" />
                            </StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="100,0,0,0" >
                                    <RadioButton GroupName="BusinessProcessTitle"  />
                                <TextBlock Text="ITAC's Requiring Review" FontWeight="Bold" />
                            </StackPanel>
                        </StackPanel>
                        </Border>

                        <Border Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" BorderThickness="1" BorderBrush="WhiteSmoke" CornerRadius="2" Margin="1" >
                            <StackPanel Margin="2">
                                <StackPanel Orientation="Horizontal" Margin="1">
                                    <RadioButton GroupName="BusinessProcessesGroup" />
                                    <TextBlock Text="All Business Processes" FontStyle="Italic" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="1" />
                                    <TextBlock Text="Fixed Asset" FontStyle="Italic" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="1">
                                    <RadioButton GroupName="BusinessProcessesGroup" />
                                    <TextBlock Text="Order to Cash" FontStyle="Italic" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="1">
                                    <RadioButton GroupName="BusinessProcessesGroup" />
                                    <TextBlock Text="Purchase to Pay" FontStyle="Italic" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="1">
                                    <RadioButton GroupName="BusinessProcessesGroup" />
                                    <TextBlock Text="Stock and Make" FontStyle="Italic" />
                                </StackPanel>
                                <StackPanel Orientation="Horizontal" Margin="1">
                                    <RadioButton GroupName="BusinessProcessesGroup" />
                                    <TextBlock Text="General Accounting and Reporting" FontStyle="Italic" />
                                </StackPanel>
                                <TextBlock Text=" " />
                            </StackPanel>
                        </Border>


                        <Border Grid.Row="0" Grid.Column="1" BorderThickness="1" BorderBrush="WhiteSmoke" Background="#A1B9DD" CornerRadius="2" Margin="1" >
                            <Grid >
                                <TextBlock Text="ITAC Description" FontWeight="Bold" HorizontalAlignment="Left" Margin="3" />
                                <TextBlock Text="UL-AC1" FontStyle="Italic" HorizontalAlignment="Right" Margin="3"/>
                            </Grid>
                        </Border>

                        <Border CornerRadius="2" Grid.Row="1" Grid.Column="1" BorderThickness="1" BorderBrush="WhiteSmoke">
                            <TextBlock TextWrapping="Wrap" Margin="3" 
                                   Text="Based on the asset class in the master data, the ERP system automatically calculates correct amounts for depreciation at the start of the depreciation run." />
                        </Border>
                        <Border CornerRadius="2" Grid.Row="2" Grid.Column="1" BorderThickness="1" BorderBrush="WhiteSmoke">
                            <StackPanel Orientation="Horizontal" Margin="1">
                                <TextBlock Text="Issue: " Margin="3,0,0,0" />
                                <TextBlock Text="Change Request to be created - Query Update Required" />
                            </StackPanel>
                        </Border>


                        
                    </Grid>
                    <Grid x:Name="MainDataGrid" Grid.Row="5" >
                        <data:DataGrid x:Name="datagrid" AutoGenerateColumns="False" HorizontalAlignment="Stretch" IsReadOnly="True" VerticalAlignment="Top" ItemsSource="{Binding DataList}" Height="220" Margin="5">
                            <data:DataGrid.Columns>
                                <data:DataGridTextColumn Header="#" Binding="{Binding Number}" />
                                <data:DataGridTextColumn Header="Business Process" Width="300" Binding="{Binding BusinessProcess}" />
                                <data:DataGridTextColumn Header="Sub Process" Width="300" Binding="{Binding SubProcess}"  />
                                <data:DataGridTextColumn Header="ITAC" Width="100" Binding="{Binding Itac}"  />
                                <data:DataGridTextColumn Header="Status" Width="200" Binding="{Binding Status}" />
                            </data:DataGrid.Columns>
                        </data:DataGrid>
                    </Grid>

                </Grid>
            </controls:TabItem>
            <controls:TabItem Header="User Access" />
            <controls:TabItem Header="User Provisioning" />
            <controls:TabItem Header="About" />
        </controls:TabControl>
    </Grid>
</UserControl>

MAINPAGE.XAML.CS

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            this.DataContext = new ItacViewModel();
        }
    }

数据.CS

   public class data
    {
        public int Number { get; set; }
        public string BusinessProcess { get; set; }
        public string SubProcess { get; set; }
        public string Itac { get; set; }
        public string Status { get; set; }

        public data(int number, string businessProcess, string subProcess, string itac, string status)
        {
            Number = number;
            BusinessProcess = businessProcess;
            SubProcess = subProcess;
            Itac = itac;
            Status = status;
        }
    }

ITACVIEWMODEL.CS

公共类 ItacViewModel : ViewModelBase { 公共列表数据列表 { 获取;放; }

public ItacViewModel()
{
    var dataList = new List<data>
                       {
                           new data(1, "Fixed Asset", "Depreciation", "UL-AC1", "Under Review"),
                           new data(2, "Fixed Asset", "Depreciation", "UL-AC2", "Complete"),
                           new data(3, "Order to Cash", "Invoicing", "UL-AC3", "Under Review"),
                           new data(4, "Order to Cash", "Shipping Goods", "UL-AC4", "Under Review"),
                           new data(5, "Order to Cash", "Depreciation", "UL-AC5", "Complete"),
                           new data(6, "Order to Cash", "Depreciation", "UL-AC6", "Under Review"),
                           new data(7, "Order to Cash", "Depreciation", "UL-AC7", "Complete"),
                           new data(8, "Stock and Make", "Depreciation", "UL-AC8", "Under Review"),
                           new data(9, "Stock and Make", "Depreciation", "UL-AC9", "Under Review"),
                           new data(10, "General Accounting & Reporting", "Depreciation", "UL-AC10", "Complete")
                       };
    DataList = dataList;
}

}

【问题讨论】:

    标签: c# visual-studio silverlight visual-studio-2010 xaml


    【解决方案1】:

    您应该将数据绑定到ICollectionView,而不是将您的DataGrid 绑定到List&lt;data&gt;ICollectionView 使分组、过滤等操作在某些数据上实现起来非常简单。

    看一下this关于如何通过ICollectionView在WPF中实现过滤的简单但内容丰富的文章

    编辑

    由于 Silverlight 不支持提供开箱即用的 ICollectionView 实现,请查看 thisthis 文章

    【讨论】:

    【解决方案2】:

    这是一种简单的方法:根据单选按钮的状态修改 Datagrid 列的 Visibility

    XAML:

    <data:DataGridTextColumn x:Name="colFixedAsset" Header="Status" Width="200" Binding="{Binding Status}" />
    <RadioButton x:Name="radioFixedAsset" GroupName="BusinessProcessesGroup" IsChecked="False" Checked="radioFixedAsset_Checked" Unchecked="radioFixedAsset_Unchecked" />
    

    后面的代码:

    private void radioFixedAsset_Checked(object sender, RoutedEventArgs e)
    {
        this.colFixedAsset.Visibility = System.Windows.Visibility.Visible;
    }
    
    private void radioFixedAsset_Unchecked(object sender, RoutedEventArgs e)
    {
        this.colFixedAsset.Visibility = System.Windows.Visibility.Collapsed;
    }
    

    如果您是 MVVM 纯粹主义者,则将布尔属性添加到您的视图模型并将您的单选按钮的 IsChecked 属性绑定到这些布尔值。还使用converter 将数据网格列的Visibility 属性绑定到这些布尔值。

    【讨论】:

    • 感谢您的回复。我首先尝试了简单的方法,但是当我单击单选按钮时出现异常,说明“对象引用未设置为对象的实例”另外,请原谅我在这里缺乏知识。此代码如何知道要过滤什么? I can't see how this would know to filter on all the "Fixed Assets" when the radio button is selected.再次,为缺乏知识道歉大声笑
    • 我没有明确指定它,但是您必须为每个单选按钮实现一个事件处理程序。此外,在编写代码和使用事件处理程序时,最好命名控件。请参阅更新的答案。对于您的例外情况,请提供有关您编写的代码的更多信息。
    • 啊,我明白了,谢谢。我对这一切都很陌生,所以如果我没有完全掌握某些东西,请原谅我:o)我将用我到目前为止的所有代码更新我的主要帖子,再次感谢!
    猜你喜欢
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多