簡體   English   中英

使用 Xaml 設置默認值或 combobox(盡管有綁定)

[英]Setting default or a combobox with Xaml (despite Binding)

我想將 RadCombobox 的 selectedItem 綁定到 DataContext 中的 Observable 集合。 這工作得很好。 但是現在我想為 combobox 設置默認值,如果我在數據上下文中設置變量,這也可以。 但在我看來,最好在 Xaml 中設置默認值。 這應該是可能的SelectedIndex="0"不幸的是,由於綁定處於活動狀態,因此不再起作用(未顯示默認值)。

當 SelectedItem 有綁定時,是否有設置默認值或 combobox 的選項?

<telerik:RadComboBox x:Name="radComboBox" 
                             ItemsSource="{Binding xyList, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="0" SelectedItem="{Binding Selectedxy}" 
                             HorizontalAlignment="Left" Margin="0,0,0,0"  VerticalAlignment="Top" Width="250" Height="25" Grid.Row="1">
        </telerik:RadComboBox>

.

public string Selectedxy  {get;set; }

        public void FillxyDropdown()
        {
           xyList = new ObservableCollection<string>();
            foreach (KeyValuePair<string, int> Line in model.ReadxyList())
            {
                xyList.Add(Line.Key);
            }

            //Sets the default value but isnt the desired way.
            Selectedxy = "XY3.31";
        }

我同意The One 的評論。 您絕對應該在視圖 Model 中指定默認值。

如果你仍然想在 XAML 中這樣做,你可以將SelectedItem屬性的綁定模式設置為OneWayToSource 在這種情況下, Selectedxy將在SelectedItem更改時更新,但反之則不會。

<telerik:RadComboBox x:Name="radComboBox" 
          ItemsSource="{Binding xyList, UpdateSourceTrigger=PropertyChanged}" SelectedIndex="1" 
          SelectedItem="{Binding Selectedxy, Mode=OneWayToSource}" HorizontalAlignment="Left"
          Margin="0,0,0,0"  VerticalAlignment="Top" Width="250" Height="25" Grid.Row="1">
</telerik:RadComboBox>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM