显示使用MVVM在silverlight中隐藏数据网格内的控件

时间:2014-07-12 18:30:10

标签: silverlight mvvm

我有一个如下所示的数据网格

**strong text**

 <sdk:DataGrid AutoGenerateColumns="False" Height="Auto" MaxHeight="500" 
                          HorizontalAlignment="Left" Margin="5" Name="_EmployeeGrid" 
                          ItemsSource="{Binding Path= Employees,Mode=TwoWay}"
                          HorizontalScrollBarVisibility="Auto"
                          ScrollViewer.HorizontalScrollBarVisibility="Auto"
                          ScrollViewer.VerticalScrollBarVisibility="Auto"
                          Visibility="Visible"
                          VerticalAlignment="Top" Width="Auto">
                                    <sdk:DataGrid.Columns>

                                        <sdk:DataGridTextColumn Header="Employee ID" IsReadOnly="True" Binding="{Binding Path=EmpID}"/>
                                        <sdk:DataGridTemplateColumn Header="Name" SortMemberPath="EmpID">
                                            <sdk:DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <HyperlinkButton VerticalAlignment="Center" NavigateUri="{Binding Path=EmpID, 
                                            Converter={StaticResource NavigatePropertyUriConverter}, ConverterParameter=EmpView}"
                                            Content= "{Binding Path=EmpID}" >
                                                    </HyperlinkButton>
                                                    <!--<HyperlinkButton NavigateUri= "{ Binding Converter={StaticResource navigateConv}, ConverterParameter=/Property/IssuesView?}" VerticalAlignment="Center"
                                                     Content="{Binding Path=PropertyId}"
                                                         HorizontalAlignment="Right" Margin="5"/>-->
                                                </DataTemplate>
                                            </sdk:DataGridTemplateColumn.CellTemplate>
                                        </sdk:DataGridTemplateColumn>


                                        <!--<sdk:DataGridTextColumn Header="Property #" IsReadOnly="True" Binding="{Binding Path=PropertyId}"/>-->
                                        <sdk:DataGridTextColumn Header="Address" IsReadOnly="True" Binding="{Binding Path=Address}"/>
                                        <sdk:DataGridTextColumn Header="Category" IsReadOnly="True" Binding="{Binding Path=CategoryName}"/>
                                        <sdk:DataGridTextColumn Header="Phone" IsReadOnly="True" Binding="{Binding Path=UnitId}" Visibility="Collapsed"/>



                                        <!--<sdk:DataGridTextColumn Header="Notes" Binding="{Binding Comments}" IsReadOnly="False" />-->


                                        <sdk:DataGridTemplateColumn x:Name="Notes1" Header="Notes">
                                            <sdk:DataGridTemplateColumn.CellTemplate>
                                                <DataTemplate>
                                                    <StackPanel Orientation="Horizontal" >
                                                        <TextBlock  MaxWidth="200"  TextAlignment="Left" TextWrapping= "Wrap" Text="{Binding Path=Comments, Mode=OneWay}" Width="200" />
                                                        <HyperlinkButton Name="btnEllipsis" Visibility="{Binding EllipsisVisibility,Source={StaticResource ViewModel}}" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="15" Content="..."  ToolTipService.ToolTip="Click For More" Command="{Binding NotesCommand,Source={StaticResource ViewModel}}" CommandParameter="{Binding}" >
                                                        </HyperlinkButton>


                                                    </StackPanel>
                                                </DataTemplate>
                                            </sdk:DataGridTemplateColumn.CellTemplate>
                                        </sdk:DataGridTemplateColumn>


                                </sdk:DataGrid>

和viewmodel中的关联属性如下所示 强文

string _ellipsisVisibility;

        public string EllipsisVisibility
        {
            get { return _ellipsisVisibility; }
            set
            {
                if (_ellipsisVisibility != value)
                {
                    _ellipsisVisibility = value;
                    RaisePropertyChanged("EllipsisVisibility");

                }
            }
        }

并将其设置为 EllipsisVisibility =&#34;折叠&#34 ;; 和 EllipsisVisibility =&#34;可见&#34 ;;

有两个问题 1.它无法显示或隐藏列 2.需要在几行中显示超链接按钮,或者根据某些条件隐藏几行,如asp.net中的Rowdatabound事件。

怎么办才能有人帮忙???

1 个答案:

答案 0 :(得分:0)

不幸的是,

(1)DataGrid列可见性不是依赖属性。这意味着它无法进行数据绑定。对此的一些解决方法涉及您感兴趣的subclassing the column type

(2)为了显示/隐藏特定控件,根据该行的数据,我会使用转换器。或者在该实体类型上创建一个新属性以直接绑定到。