How to make an item in the list visible but invisible

I have a Listbox associated with my view model. This view model has the property

I need the element to be visible, but not related to changing this field. Any suggestion to anyone

public interface IRegionAreaDM 
{

    /// <summary>
    /// Name of the Focus Area
    /// </summary>
    string RegionAreaName { get; set; }

    /// <summary>
    /// Determines if the Tab is currently selected.
    /// </summary>
    bool IsSelected { get; set; }

    /// <summary>
    /// Determines if the Tab is linked to any other Tab
    /// </summary>
    bool IsLinked { get; set; }

    /// <summary>
    ///
    /// </summary>
    bool IsActive { get; set; }

}

Each element is associated with an element in XAML, for example, Name with a text box. IsSelected with CheckBox and IsActive should make ListBoxItems enabled / disabled depending on the logic and my Xaml style calls something like this

 <Style TargetType="ListBoxItem">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="ListBoxItem">
            <Border x:Name="Bd" HorizontalAlignment="Stretch" Background="#00D05252"
                    BorderThickness="0,1" SnapsToDevicePixels="true">
              <!--  <StackPanel x:Name="ParamterRoot" Orientation="Horizontal">  -->
              <Grid x:Name="ParamterRoot">
                <Grid.ColumnDefinitions>
                  <ColumnDefinition Width="Auto" />
                  <ColumnDefinition Width="*" />
                  <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <CheckBox x:Name="ParametersCheckbox" Grid.Column="0" Margin="10,0,0,0"
                          VerticalAlignment="Center" IsChecked="{Binding IsSelected}"
                <TextBlock Grid.Column="1" Width="Auto" Margin="20,7.5,0,7.5"
                           Text="{Binding RegionAreaName}" TextTrimming="CharacterEllipsis">
                  <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                      <Style.Triggers>
                        <Trigger Property="IsMouseDirectlyOver" Value="True">
                          <Setter Property="Cursor" Value="Hand" />
                        </Trigger>
                      </Style.Triggers>
                    </Style>
                  </TextBlock.Style>
                </TextBlock>                   
              </Grid>
              <!--  </StackPanel>  -->
            </Border>
            <ControlTemplate.Triggers>
              <Trigger Property="IsMouseOver" Value="true">
                <Setter TargetName="Bd" Property="Background" Value="#FFC10000" />
              </Trigger>
              <Trigger Property="IsSelected" Value="true">
                <Setter TargetName="Bd" Property="Background" Value="#FFC10000" />
              </Trigger>
 <DataTrigger Binding="{Binding IsActive}" Value="False">
                                    <Setter Property="IsEnabled" Value="False" />
                                </DataTrigger>

            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
+3
source share
3 answers

enter image description here

Code Behind: (you use MVVM to tweak this code a bit, but to fit your template)

public partial class Window1 : Window
{
    public List<T> Items { get; set; }
    public Window1()
    {
        Items = new List<T>
                    {
                        new T{  Id = 1,Name = "qwe",IsEnabled = true},
                        new T{  Id = 2,Name = "asd",IsEnabled = false},
                        new T{  Id = 3,Name = "zxc",IsEnabled = true},
                        new T{  Id = 4,Name = "rty",IsEnabled = false},
                    };
        InitializeComponent();            
        DataContext = this;
    }
}

public class T
{
    public int Id { get; set; }
    public String Name { get; set; }
    public bool IsEnabled { get; set; }
}

XAML:

<ListBox Name="listBox1" ItemsSource="{Binding Items}" DisplayMemberPath="Name">
    <ListBox.ItemContainerStyle>
        <Style TargetType="{x:Type ListBoxItem}">
            <Setter Property="IsEnabled" Value="{Binding IsEnabled,Mode=TwoWay}" />
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Hope this help

+2
source

- , ! IsEnabled .

<Style TargetType="ListBoxItem">
   <Setter Property="IsEnabled" Value="True" /><!-- default -->
   ...from here your stuff
+1

An alternative to configuring IsEnabled woud should be to use a trigger to set the UIElement.IsHitTestVisible property.

 <DataTrigger Binding="{Binding IsActive}" Value="False"> 
    <Setter Property="IsHitTestVisible" Value="False" /> 
 </DataTrigger> 
+1
source

All Articles