简体   繁体   English

如何在绑定WPF中使用if表达式-C#

[英]How to use an if expression in Binding WPF - c#

Here is my code: 这是我的代码:

<ListView Grid.Row="1" x:Name="viewTicket" Style="{StaticResource ticketListBox}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" Background="Transparent" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Center" BorderBrush="{x:Null}" SelectionChanged="ViewTicket_SelectionChanged">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="4*"/>
                        <ColumnDefinition Width="2*"/>
                    </Grid.ColumnDefinitions>
                    <Image Visibility="{Binding selectedCheck}" Name="check" Grid.Column="0" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Center" Source="../../Images/check-donatota.png" Stretch="None" MouseLeftButtonUp="Check_MouseLeftButtonUp"/>
                    <TextBlock Visibility="{Binding selectedQuantity}" Name="quantity" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding amount}"/>
                    <TextBlock Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Foreground="Black" TextWrapping="Wrap" Text="{Binding name}"/>
                    <TextBlock Visibility="{Binding selectedPrice}" Name="price" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding total, StringFormat=C}"/>
                    <Image Visibility="{Binding selectedTrash}" Name="trash" Grid.Column="2" Margin="0,0,15,0" HorizontalAlignment="Right" VerticalAlignment="Center" Source="../../Images/trash-donatota.png" Stretch="None" MouseLeftButtonUp="Trash_MouseLeftButtonUp"/>
                </Grid>

                <ListView 
                    ItemsSource="{Binding ingredients}" 
                    Grid.Row="1" 
                    Margin="-5,0,0,0" 
                    Name="viewTicketIngs" 
                    IsHitTestVisible="False" 
                    Style="{StaticResource ticketListBox}"
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled" 
                    ScrollViewer.VerticalScrollBarVisibility="Hidden" 
                    Background="Transparent" 
                    HorizontalContentAlignment="Stretch" 
                    VerticalContentAlignment="Center" 
                    BorderBrush="{x:Null}" 
                    SelectionChanged="ViewTicketIngs_SelectionChanged">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="4*"/>
                                    <ColumnDefinition Width="2*"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Visibility="Visible" Name="quantity" Grid.Column="0" Foreground="{DynamicResource GrayTextDonaTotaBrush}" HorizontalAlignment="Center" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding amount}"/>
                                <TextBlock Margin="10,0,0,0" Grid.Column="1" Foreground="{DynamicResource GrayTextDonaTotaBrush}" HorizontalAlignment="Left" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding ing.name}"/>
                                <TextBlock Visibility="Visible" Name="price" Grid.Column="2" Foreground="{DynamicResource GrayTextDonaTotaBrush}" HorizontalAlignment="Right" VerticalAlignment="Center" TextWrapping="Wrap" Text="{Binding total, StringFormat=C}"/>
                            </Grid>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

I add data to the ListView viewTicket , but depending of a property I would like to change the ItemSource Binding of the ListView viewTicketIngs . 我将数据添加到ListView viewTicket ,但是根据属性,我想更改ListView viewTicketIngsItemSource绑定。 In other words, is there anyway that I can use an if expression on the binding? 换句话说,无论如何,我可以在绑定上使用if表达式吗? Something like ItemsSource="{Binding IF(mode == 0) {ingredients} else {plates}}" ItemsSource="{Binding IF(mode == 0) {ingredients} else {plates}}"

Change the Binding by a DataTrigger in a Style: 通过样式更改DataTrigger的绑定:

<ListView ...>
    <ListView.Style>
        <Style TargetType="ListView">
            <Setter Property="ItemsSource" Value="{Binding plates}"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding mode}" Value="0">
                    <Setter Property="ItemsSource" Value="{Binding ingredients}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ListView.Style>
</ListView>

As I understand it, you sometimes dispaly plates, sometimes ingredients. 据我了解,您有时会分配盘子,有时会配料。 Now there are triggers conditional display. 现在有触发器条件显示。 WPF actually has a pretty wide support. WPF实际上有相当广泛的支持。

However what might be better is to have 2 different ViewModel classes and two (how I call them) "type targetting data tempaltes". 但是,最好有两个不同的ViewModel类和两个(我如何称呼它们)“类型定位数据模板”。 Say you have these clases: 假设您有以下几种情况:

abstract class ViewModelItem { }

class Plate : ViewModelItem { }

class IngredientsList : ViewModelItem { }

The propety you exposie this in, would be set to ViewModelItem . 您将对此进行揭露的内容将设置为ViewModelItem In realtiy you would assign either a Plate or IngredientsList Instance. 实际上,您将分配一个PlateIngredientsList实例。

Now you define two DataTemplates. 现在,您定义了两个DataTemplates。 A interesting thing about WPF is that if you do not specify a explicit Template, the Code will go out of it's way to try to find one. 关于WPF的一件有趣的事情是,如果您不指定显式模板,则该代码将无法找到一个模板。 And it will do the matching via the DataType property of the Template (TargetType for Styles and similar). 它将通过TemplateDataType属性 (样式和类似名称为TargetType)进行匹配。 It works similar to what CSS does, with somebodies code going out of it's way to find a template to apply. 它的工作方式与CSS相似,有些代码会从中找到要应用的模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM