亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以在 xaml 代碼中禁用 WPF 組合框的第一項嗎?

我可以在 xaml 代碼中禁用 WPF 組合框的第一項嗎?

C#
胡子哥哥 2022-12-31 14:01:59
考慮以下組合框:<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>組合框的第一個項目是默認預設,在文本框中具有相應的默認名稱。因此,用戶不應該能夠對第一項進行輸入 - 因此我想禁用第一項的文本框。我知道我可以在包含類或視圖模型的構造函數中運行一個函數,它會禁用第一個索引上的文本框,但是我想知道這是否可以直接在 xaml 代碼中實現(我會發現它更優雅做這種靜態事情的方式)。
查看完整描述

2 回答

?
慕的地10843

TA貢獻1785條經驗 獲得超8個贊

您可以利用PreviousData RelativeSource 將為null集合的第一個元素返回這一事實。知道您可以將DataTrigger添加到 DataTemplate 以將其IsEnabled屬性設置TextBox為 false。


這是ItemTemplate帶有PreviousData綁定的簡化版本:


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


查看完整回答
反對 回復 2022-12-31
?
www說

TA貢獻1775條經驗 獲得超8個贊

我用DataBindingfor創建了一個復合集合,ListView但邏輯是一樣的:


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

這樣你就可以讓第一個項目不可選。我也會使用SelectedValue而不是SelectedIndex.


查看完整回答
反對 回復 2022-12-31
  • 2 回答
  • 0 關注
  • 86 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號