使用单选按钮C#在Silverlight Datagrid上过滤

时间:2012-01-09 09:50:00

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

我对Silverlight和C#/ XAML有点新,我有一个我无法弄清楚的问题。

我有一个datagrid:

<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>

到目前为止,我还没有能够完成这项工作,有人可以给我一些指示吗?

先谢谢

的Dom

EDIT 到目前为止,这是我的所有代码: 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();
        }
    }

DATA.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 List DataList {get;组; }

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;
}

}

2 个答案:

答案 0 :(得分:0)

您应该将数据绑定到ICollectionView,而不是将DataGrid绑定到List<data>ICollectionView使得分组,过滤等操作非常简单,无法在某些数据上实现。

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

修改

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

答案 1 :(得分:0)

这是一个微不足道的方法:根据您的无线电按钮的状态修改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纯粹主义者,则将布尔属性添加到视图模型中,并将您的radiobuttons的IsChecked属性绑定到那些布尔值。还可以使用converter将datagrid列的Visibility属性绑定到这些布尔值。