开发者

How to Display "No Items found" in a Datagrid

开发者 https://www.devze.com 2023-03-30 15:00 出处:网络
<dg:DataGrid.Resources> <ViewModel:SmartMessengerx:Key=\"Noitemsfound\"> </ViewModel:SmartMessenger>
               <dg:DataGrid.Resources>
                        <ViewModel:SmartMessenger  x:Key="Noitemsfound">
                        </ViewModel:SmartMessenger>
                      </dg:DataGrid.Resources>
                 <dg:DataGrid.RowDetailsTemplate>
                        <DataTemplate>
                          <StackPanel>`enter code here`
                            <TextBlock Text="{Binding Source={StaticResource         Noitemsfound }, Path=pNorecords,Mode=TwoWay}" />
                          </StackPanel>
                        </DataTemplate>
                 </dg:DataGrid.RowDetailsTemplate>

Here i am trying to bind a textblock that will display a message when there are no items in the datagrid . I am using VS 2008 Express edition. The problem is am not able to bind the property Noitemsfound with a code behind cl开发者_如何学Goass SmartMessenger ... Whats missing here???


if you just want to display a message in your datagrid when there are no items - you can do this with a style. Put this style in your App.xaml Resources or at least in your datagrid Resources.

    <Style x:Key="{x:Type ItemsControl}" TargetType="{x:Type ItemsControl}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding Items.Count, RelativeSource={RelativeSource Self}}" Value="0">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush Stretch="None">
                        <VisualBrush.Visual>
                            <TextBlock Text="no items" 
                                       FontFamily="{StaticResource FontFamily}"
                                       FontSize="{StaticResource FontSize}"/>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Setter.Value>
            </Setter>
        </DataTrigger>
        <DataTrigger Binding="{Binding Items, RelativeSource={RelativeSource Self}}" Value="{x:Null}">
            <Setter Property="Background">
                <Setter.Value>
                    <VisualBrush Stretch="None">
                        <VisualBrush.Visual>
                            <TextBlock Text="no items" 
                                       FontFamily="{StaticResource FontFamily}"
                                       FontSize="{StaticResource FontSize}"/>
                        </VisualBrush.Visual>
                    </VisualBrush>
                </Setter.Value>
            </Setter>
        </DataTrigger>
    </Style.Triggers>
</Style>
<Style x:Key="{x:Type DataGrid}" TargetType="{x:Type DataGrid}" BasedOn="{StaticResource {x:Type ItemsControl}}">
</Style>


Check output window. WPF dumps binding error/exception occurred to output windows. There you gets the chance to verify if it is binding to right source, if path is correct etc.

You can also try adding dummy converter in the binding expression. Converter provides you chance to debug binding expression

0

精彩评论

暂无评论...
验证码 换一张
取 消