将ComboBox SelectedValue绑定到字符串会禁用默认的SelectedValue wpf(Binding ComboBox SelectedValue to string disabl

编程入门 行业动态 更新时间:2024-10-16 20:29:23
将ComboBox SelectedValue绑定到字符串会禁用默认的SelectedValue wpf(Binding ComboBox SelectedValue to string disables the default SelectedValue wpf)

我正在尝试将ComboBox SelectedValue绑定到string 。 Binding工作完美无瑕。 但是,我ComboBoxItem's IsSelected一个ComboBoxItem's IsSelected设置为True ,但由于某些原因,当我启动应用程序时,没有选择任何项目, SelectedValue为空,我需要重新选择我想要的项目。

这是我的代码:

XAML:

<ComboBox x:Name="SearchOptions" FontFamily="Times New Roman" Foreground="DarkRed" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Column="2" Margin="10,0,0,0" Height="20" SelectedValue="{Binding SearchType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <ComboBoxItem x:Name="Contact" Content="A" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalContentAlignment="Center" IsSelected="True"/> <ComboBoxItem x:Name="Paper" Content="B" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalContentAlignment="Center"/> </ComboBox>

ViewModel Code-Behind:

private string m_serachType; public string SearchType { get { return m_serachType; } set { m_serachType = value; OnPropertyChanged("SearchType"); } }

我的ViewModel类实现了INotifyPropertyChanged 。

有任何想法吗?

I'm trying to bind a ComboBox SelectedValue to a string. The Binding works flawlessly. However, one of my ComboBoxItem's IsSelected is set to True, but for some reason when I launch the application, none of the items is selected, the SelectedValue is blank and I need to re-select the item I want.

Here's my code:

XAML:

<ComboBox x:Name="SearchOptions" FontFamily="Times New Roman" Foreground="DarkRed" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Grid.Column="2" Margin="10,0,0,0" Height="20" SelectedValue="{Binding SearchType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <ComboBoxItem x:Name="Contact" Content="A" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalContentAlignment="Center" IsSelected="True"/> <ComboBoxItem x:Name="Paper" Content="B" FontFamily="Times New Roman" Foreground="DarkRed" HorizontalContentAlignment="Center"/> </ComboBox>

ViewModel Code-Behind:

private string m_serachType; public string SearchType { get { return m_serachType; } set { m_serachType = value; OnPropertyChanged("SearchType"); } }

My ViewModel class implements INotifyPropertyChanged.

Any ideas?

最满意答案

尝试使用ComboboxItem string :

主窗口(XAML)

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Grid> <ComboBox SelectedItem="{Binding SearchType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" > <sys:String>A</sys:String> <sys:String>B</sys:String> </ComboBox> </Grid> </Window>

MainWindow(cs)

public MainWindow() { InitializeComponent(); this.DataContext = new MyViewModel() { SearchType = "A" }; }

MyViewModel

class MyViewModel : INotifyPropertyChanged { private string m_serachType; public string SearchType { get { return m_serachType; } set { m_serachType = value; OnPropertyChanged("SearchType"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } }

Try using string insted of ComboboxItem:

MainWindow(XAML)

<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Grid> <ComboBox SelectedItem="{Binding SearchType, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" > <sys:String>A</sys:String> <sys:String>B</sys:String> </ComboBox> </Grid> </Window>

MainWindow (cs)

public MainWindow() { InitializeComponent(); this.DataContext = new MyViewModel() { SearchType = "A" }; }

MyViewModel

class MyViewModel : INotifyPropertyChanged { private string m_serachType; public string SearchType { get { return m_serachType; } set { m_serachType = value; OnPropertyChanged("SearchType"); } } public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } }

更多推荐

本文发布于:2023-08-04 23:06:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1423384.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   绑定   ComboBox   SelectedValue   wpf

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!