简体   繁体   English

我可以在 xaml 代码中禁用 WPF 组合框的第一项吗?

[英]Can I disable the first item a WPF combobox within xaml code?

Consider the following combobox:考虑以下组合框:

<ComboBox ItemsSource="{Binding Presets.VolumePresetList}" SelectedIndex="{Binding VolumePresetSelectedIndex, UpdateSourceTrigger=PropertyChanged}" Margin="10, 10" HorizontalAlignment="Left"
            MinWidth="150">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBox Text="{Binding PresetName, UpdateSourceTrigger=Explicit}" VerticalAlignment="Center" Height="20" BorderThickness="0" LostFocus="TextBox_LostFocus" KeyUp="TextBox_KeyUp"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

The first Item of the combobox is a default preset with a corresponding default name in the textbox.组合框的第一个 Item 是默认预设,在文本框中具有相应的默认名称。 The user therefore shouldn't be able to make input to this first item - thus I want to disable the textbox of the first item.因此,用户不应该能够对第一个项目进行输入 - 因此我想禁用第一个项目的文本框。

I know I could just run a function in the constructor of the containing class or the viewmodel, which disables the textbox on the first index, however I'm wondering if this is possible directly within the xaml code (which I would find a more elegant way of doing such static things).我知道我可以在包含类或视图模型的构造函数中运行一个函数,它禁用第一个索引上的文本框,但是我想知道这是否可以直接在 xaml 代码中实现(我会发现它更优雅)做这种静态事情的方式)。

You could make use of the fact that the PreviousData RelativeSource will return null for the first element of a collection.您可以利用PreviousData RelativeSource 将为集合的第一个元素返回null的事实。 Knowing that you can add a DataTrigger to your DataTemplate to set the IsEnabled property of the TextBox to false.知道您可以将DataTrigger添加到您的 DataTemplate 以将TextBoxIsEnabled属性设置为 false。

Here is a simplified version of the ItemTemplate with a PreviousData binding:这是带有PreviousData绑定的ItemTemplate的简化版本:

       <ComboBox.ItemTemplate>
            <DataTemplate>
                <TextBox x:Name="TextBox" />
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=PreviousData}}"
                                 Value="{x:Null}">
                        <Setter TargetName="TextBox" Property="IsEnabled" Value="False" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ComboBox.ItemTemplate>

I have created a composite collection with DataBinding for ListView but the logic will be the same:我已经为ListView创建了一个带有DataBinding的复合集合,但逻辑是相同的:

<ListView SelectedValue="{Binding youVMPropertyHere}">
    <ListView.ItemsSource>
        <CompositeCollection>
            <ListViewItem IsHitTestVisible="False">Default Item</ListViewItem>
            <CollectionContainer Collection="{Binding Source={StaticResource cvsPresetLists}}"/>
        </CompositeCollection>
    </ListView.ItemsSource>
<!-- Where-->
<Window.Resources>
    <CollectionViewSource Source="{Binding Presets.VolumePresetList}" x:Key="cvsPresetLists"/>
</Window.Resources>  

This way you can have the first item to be not selectable.这样你就可以让第一个项目不可选。 I would also use SelectedValue instead of SelectedIndex .我也会使用SelectedValue而不是SelectedIndex

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

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