Emir Can Aydın
Emir Can Aydın

Reputation: 60

How to change the border's style according to the object's occurrence?

I created a ListView. In this list view, each rectangle represents the tables in a business. These tables can have a receipt. I made the style {StaticReseource closeTableBorderStyle} for tables that do not have a receipt. But if the table has an addition, I want to change the style of the table.

closeTableBorderStyle code:

<Style x:Key="closeTableBorderStyle" TargetType="Border">
        <Setter Property="Height" Value="100"/>
        <Setter Property="Background" Value="#0a7d38"/>
        <Setter Property="CornerRadius" Value="8"/>
        <Setter Property="Margin" Value="5,5,0,0"/>
        <Setter Property="Cursor" Value="Hand"/>
        <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="#00d052"/>
            </Trigger>
        </Style.Triggers>
    </Style>

Table class:

public class Table
    {
        public int tableID { get; set; }
        public string tablename { get; set; }
        public List<OrderReceipt> tableReceipt { get; set; }
    }

ListView:

          x:Name="ListViewTableBorder"
          SelectionChanged="ListViewTableBorder_SelectionChanged" 
          HorizontalContentAlignment="Stretch" 
          VerticalContentAlignment="Stretch">
   <ListView.ItemsPanel>
      <ItemsPanelTemplate>
         <UniformGrid Columns="5"/>
      </ItemsPanelTemplate>
   </ListView.ItemsPanel>
   <ListView.ItemContainerStyle>
      <Style TargetType="{x:Type ListViewItem}">
         <Setter Property="Background" Value="Transparent" />
         <Setter Property="BorderBrush" Value="Transparent"/>
         <Setter Property="VerticalContentAlignment" Value="Center"/>
         <Setter Property="Template">
            <Setter.Value>
               <ControlTemplate TargetType="{x:Type ListViewItem}">
                  <Grid Background="{TemplateBinding Background}">
                     <Border Name="Selection" Visibility="Collapsed" />
                     <Border Style="{StaticResource sales_border}">
                        <TextBlock Text="{Binding tablename}" 
                                   HorizontalAlignment="Center" 
                                   VerticalAlignment="Center"
                                   Foreground="White"
                                   FontSize="20"/>
                     </Border>
                     <GridViewRowPresenter Grid.RowSpan="2"
                                           Margin="{TemplateBinding Padding}"
                                           HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                           VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                           SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                  </Grid>
               </ControlTemplate>
            </Setter.Value>
         </Setter>
      </Style>
   </ListView.ItemContainerStyle>
</ListView>

It came to the Table class to define a boolean variable named isTableOpen and then write an algorithm like below, but I couldn't.

if isTableOpen = false
{
   style -> closeTableBorder
}
else
{
   stlye -> openTableBorder
}

In summary, if tableReceipt.Count > 0, I want the border style to be openBorderTableStyle. Else tableReceipt.Count < 0, I want the border style to be closeBorderTableStyle.

Upvotes: 1

Views: 118

Answers (1)

thatguy
thatguy

Reputation: 22119

You can do this by giving the target Border a name like TableNameBorder and add a trigger to the template that sets the Style of the border to closeBorderTableStyle when isTableOpen is false.

<ControlTemplate TargetType="{x:Type ListViewItem}">
   <Grid Background="{TemplateBinding Background}">
      <Border Name="Selection" Visibility="Collapsed" />
      <Border x:Name="TableNameBorder" Style="{StaticResource sales_border}">
         <TextBlock Text="{Binding tablename}" 
                    HorizontalAlignment="Center" 
                    VerticalAlignment="Center"
                    Foreground="White"
                    FontSize="20"/>
      </Border>
      <GridViewRowPresenter Grid.RowSpan="2"
                            Margin="{TemplateBinding Padding}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
   </Grid>
   <ControlTemplate.Triggers>
      <DataTrigger Binding="{Binding isTableOpen}" Value="False">
         <Setter TargetName="TableNameBorder" Property="Style" Value="{StaticResource closeTableBorderStyle}"/>
      </DataTrigger>
   </ControlTemplate.Triggers>
</ControlTemplate>

Consider implementing INotifyPropertyChanged in your Table type if you intend to change the properties, otherwise the changes will not be reflected in the user interface. Likewise, if you want to modify your collections at runtime, you should use ObservableCollection<T> instead of List<T>.

Upvotes: 1

Related Questions