使用 Xaml 设置默认值或组合框(尽管有绑定)
Setting default or a combobox with Xaml (despite Binding)
我想将 RadCombobox 的 selectedItem 绑定到 DataContext 中的 Observable 集合。这工作得很好。
但现在我想为组合框设置默认值,如果我在数据上下文中设置变量,这也有效。但我认为将默认值设置在 Xaml 内会更好。这应该可以用 SelectedIndex="0"
不幸的是它不再工作(没有显示默认值),因为绑定是活动的。
当 SelectedItem 有绑定时,是否有设置默认值或组合框的选项?
<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";
}
我同意 评论。您绝对应该在视图模型中指定默认值。
如果您还想在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>
我想将 RadCombobox 的 selectedItem 绑定到 DataContext 中的 Observable 集合。这工作得很好。
但现在我想为组合框设置默认值,如果我在数据上下文中设置变量,这也有效。但我认为将默认值设置在 Xaml 内会更好。这应该可以用 SelectedIndex="0"
不幸的是它不再工作(没有显示默认值),因为绑定是活动的。
当 SelectedItem 有绑定时,是否有设置默认值或组合框的选项?
<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";
}
我同意
如果您还想在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>