gpt4 book ai didi

wpf - 如何使列表框中的项目可见但不可点击

转载 作者:行者123 更新时间:2023-12-04 14:27:04 27 4
gpt4 key购买 nike

我有一个绑定(bind)到我的 View 模型的列表框。这个 View 模型有一个属性

我需要随着该字段的更改使项目可见但不可点击。任何建议任何人

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; }

每个项目都连接到 XAML 中的一个项目,例如带有文本框的名称。 IsSelected with a CheckBox and IsActive 是根据逻辑启用/禁用 ListBoxItems我的 Xaml 样式看起来像这样

 <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>

最佳答案

enter image description here

代码隐藏:(您正在使用 MVVM,因此您将稍微调整此代码以适应您的模式)

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>

希望对您有所帮助

关于wpf - 如何使列表框中的项目可见但不可点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10492948/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com