我的问题基本上就是这个问题.我认为这将有助于提供一些更容易重现问题的信息和代码.
使用RibbonControlsLibrary中的Microsoft.Windows.Controls.Ribbon.RibbonComboBox 感觉就像走过一个充满了bug的大沼泽,而不是你知道如何绕过它做的事情.
Anywho.我遇到的最大问题是数据绑定我的SelectedItem.
以下是我的开始(在我发现RibbonGallery之后?).在ComboBox的子元素上使用ItemsSource和SelectedItem,甚至在同一级别上已经给了我heebie-jeebies,但这似乎是正确的.
在示例应用程序中,我在ViewModel的构造函数中设置SelectedItem.但是,在运行应用程序时,不会显示SelectedItem.甚至VS设计师也正确地展示了"第二选择"!
正在运行应用
VS设计师: 
调试SelectedItem setter时,您会注意到多次传递.在第一次将它设置为ctor中的"第二个选项"(1,参见下面的调试日志)后,它将重置为null(2)(通过外部代码,我在控件本身中计算).在UI中打开下拉列表时,它将再次设置为null(3),然后在选择值时,将此值设置为两倍(4,5).我选择了"第二选项",然后用"第一选项"(6-9)重复该过程.这产生了以下日志(忽略了功能区控件中的一千零一个绑定异常...):

显而易见的大问题是(2),这是重置我的初始选择.看起来当第一次显示控件时,它会被重置.一个非常丑陋的解决方法是通过计时器设置值.在用户控件的Loaded事件中设置它在这个示例应用程序中对我有用,但在我较重的现实应用程序中,它没有.无论如何,所有这些都是错误的.有谁知道更好的解决方案?
XAML:
<UserControl x:Class="WpfApplication1.RibbonComboBoxDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:r="http://schemas.microsoft.com/winfx/2006/xaml/presentation/ribbon"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.DataContext>
<local:ViewModel />
</UserControl.DataContext>
<Grid>
<r:Ribbon >
<r:RibbonTab Header="First Tab">
<r:RibbonGroup Header="Group">
<r:RibbonComboBox >
<r:RibbonGallery SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
<r:RibbonGalleryCategory ItemsSource="{Binding Controls}" DisplayMemberPath="Caption" />
</r:RibbonGallery>
</r:RibbonComboBox>
</r:RibbonGroup>
</r:RibbonTab>
<r:RibbonTab Header="Second Tab" />
</r:Ribbon>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
视图模型:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
namespace WpfApplication1
{
public class ViewModel …Run Code Online (Sandbox Code Playgroud) 我在我的WPF应用程序中使用ComboBox并跟随MVVM.我想在ComboBox中显示一个字符串列表.
XAML:
<ComboBox ItemsSource="{Binding ItemsCollection}" SelectedItem="{Binding SelectedItem}" />
Run Code Online (Sandbox Code Playgroud)
查看型号:
public Collection<string> ItemsCollection; // Suppose this has 10 values.
private string _selectedItem;
public string SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
Trigger Notify of property changed.
}
}
Run Code Online (Sandbox Code Playgroud)
现在这段代码工作得很好.我可以从视图中进行选择,我可以在ViewModel中进行更改,如果我从ViewModel更改SelectedItem,我可以在我的视图中看到它.
现在这就是我想要实现的目标.当我从我的视图中更改所选项目时,我需要检查值是好/坏(或任何)设置所选项目,否则不设置它.所以我的视图模型就像这样改变了.
public string SelectedItem
{
get { return _selectedItem; }
set
{
if (SomeCondition(value))
_selectedItem = value; // Update selected item.
else
_selectedItem = _selectedItem; // Do not update selected item.
Trigger Notify of property changed.
} …Run Code Online (Sandbox Code Playgroud) 我有一个特定的xaml数据绑定问题.我有两个列表框(主 - 详细信息,因此列表框将IsSynchronizedWithCurrentItem设置为true).我希望我的viewmodel知道详细信息列表框中的所选项何时更改:我在viewmodel类上创建了一个int属性(即我们可以调用此属性SelInd)并在详细信息viewmodel上以这种方式绑定:
SelectedIndex="{Binding Mode=OneWayToSource, Path=SelInd}"
Run Code Online (Sandbox Code Playgroud)
我在运行时没有得到任何错误/异常,但绑定不会触发:当所选项目发生更改时,我的viewmodel属性不会更新.如果我将绑定模式更改为TwoWay,一切正常,但这不是我需要的.我需要它与OneWayToSource一起使用(顺便说一句,如果我将SelectedItem绑定到SelectedValue属性,则适用相同的非工作行为).
为什么这些绑定不会触发OneWayToSource?
这是一个更完整的代码示例,只是为了让事情更清楚:编辑:我无法显示真实代码(NDA)但我会在这里展示更简单和类似的东西(Page的DataContext是一个PageViewModel类的实例解释我只需要我的viewmodel类的SelInd属性应该始终在第二个ListBox中反映SelectedIndex的值.我已经找到了这样做的替代方法(代码隐藏中的事件处理程序或附加行为)但是现在我只是好奇它为什么它不适用于OneWayToSource绑定.
<Page>
<ContentControl x:Name="MainDataContext">
<Grid DataContext={Binding Path=Masters}>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<ListBox Grid.Column="0"
SelectionMode="Single"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding }">
<ListBox.ItemContainerStyle>
...
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
....
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListBox Grid.Column="1"
SelectionMode="Single"
SelectedIndex="{Binding Mode=OneWayToSource, ElementName=MainDataContext,Path=DataContext.SelInd}"
IsSynchronizedWithCurrentItem="True"
ItemsSource="{Binding Path=Details}">
<ListBox.ItemContainerStyle>
...
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
....
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</ContentControl>
</Page>
Run Code Online (Sandbox Code Playgroud)
这是视图模型类的草图
public class PageViewModel{
public ObservableCollection<MasterClass> Masters {get;set;}
public int SelInd {get;set;}
....
}
Run Code Online (Sandbox Code Playgroud)
这里是MasterClass,它只是一个名字和一个细节列表
public class MasterClass{
public ObservableCollection<DetailsClass> Details …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码将值添加到与显示的组合框不同的组合框:如何向组合框项添加值
假设我的Combobox中有3个值:
item 1
item 2
item 3
Run Code Online (Sandbox Code Playgroud)
如果我item 2从下拉列表中选择链接中的代码.但是,如果我item 2手动TYPE 它不起作用,因为我认为键入它只设置combobox1.text值而不是combobox1.selecteditem.我可以输入下拉列表中存在的值,或者不存在的值.如果我输入一个存在的,那么selectedItem属性也应该设置为正确的值.可以这样做吗?
谢谢
我有一个ListBox水平滚动图像.
我有以下XAML我用混合来创建它.它最初有斧头:关键Style TaregetType在线,MSDN说要删除它,因为我得到了错误.现在我收到这个错误:
Error 3 Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
我不明白如何以这种方式应用所有这些垃圾,我尝试了几件事,没有任何工作.
我的目标是让所选项目的背景为白色,而不是蓝色.对于这么小的东西来说似乎很多工作!
谢谢.
<ListBox ItemsSource="{Binding Source={StaticResource WPFApparelCollection}}"
Grid.Row="1" Margin="2,26,2,104" ScrollViewer.VerticalScrollBarVisibility="Hidden"
ScrollViewer.HorizontalScrollBarVisibility="Hidden" SelectionMode="Single"
x:Name="list1" MouseLeave="List1_MouseLeave" MouseMove="List1_MouseMove" Style="{DynamicResource ListBoxStyle1}" >
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Background" Value="Transparent"/>
<Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
<Setter Property="Padding" Value="2,0,0,0"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Border x:Name="Bd" SnapsToDevicePixels="true" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding …Run Code Online (Sandbox Code Playgroud) 我有一个实现的viewmodel INotifyPropertyChanged.在这个viewModel上是一个名为的属性SubGroupingView.此属性绑定到组合框的选定项.当我更改组合框时,源属性正在更新,但是当我更改源属性或初始化控件时,combobox.selectedItem它不反映属性中存在的内容.
以下是一些可以帮助您入门的代码:
<ComboBox Grid.Column="3" Grid.Row="1"
Margin="0,1,4,1"
SelectedItem="{Binding Path=SubGroupingView, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, diag:PresentationTraceSources.TraceLevel=High}"
ItemsSource="{Binding Columns}"
DisplayMemberPath="DisplayName">
Run Code Online (Sandbox Code Playgroud)
该属性引发PropertyChanged事件,TraceSource输出显示绑定检测到它并传输了值,只是组合框没有反映它.任何想法都会受到欢迎!
编辑:
跟踪源的输出是这样的:
System.Windows.Data Warning: 91 : BindingExpression (hash=23631369): Got PropertyChanged event from ReportViewModel (hash=52844413)
System.Windows.Data Warning: 97 : BindingExpression (hash=23631369): GetValue at level 0 from ReportViewModel (hash=52844413) using RuntimePropertyInfo(SubGroupingView): DataColumnViewModel (hash=58231222)
System.Windows.Data Warning: 76 : BindingExpression (hash=23631369): TransferValue - got raw value DataColumnViewModel (hash=58231222)
System.Windows.Data Warning: 80 : BindingExpression (hash=23631369): TransferValue - implicit converter produced DataColumnViewModel (hash=58231222) …Run Code Online (Sandbox Code Playgroud) 我有两个用户控件,第一个带有一个列表框,该列表框绑定到一个客户列表,显示每个客户的一些简单细节.
第二个用户控件我想更详细地查看在第一个usercontrol的列表框中选择的客户.
是否可以在第二个控件中设置绑定以绑定到第一个用户控件中的选定项?
我的列表框:
<ListBox Name="lstCustomer" ItemsSource="{Binding Customers}" >
<ListBox.Resources>
<DataTemplate DataType="{x:Type MyApplication:Customers}">
<Label Grid.Row="0" Content="{Binding Customer.name}" FontSize="14" FontWeight="Bold" Padding="5" />
<Label Grid.Row="1" Grid.Column="0" Content="{Binding Customer.telephone}" Padding="10,5" />
</Grid>
</Grid>
</DataTemplate>
</ListBox.Resources>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
详细视图Usercontrol(迄今为止)
<Grid x:Name="containingGrid" DataContext="{Binding ElementName=lstCustomers, Path=SelectedItem}">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding Customer.name}" FontSize="23"/>
</Grid>
Run Code Online (Sandbox Code Playgroud)
谢谢格雷格
有2个RadioButtonLists:
Are you a minor?: oYes oNo
Do you wish a joint account?: oYes oNo
Run Code Online (Sandbox Code Playgroud)
如果第一个问题的答案是肯定的,我想检测对第一个问题的回答,并使用jQuery函数将第二个问题的答案设置为yes.提前致谢.
<asp:Label ID="lblMinfor" CssClass="boldIt" runat="server" Text="Is this account for a minor" Style="float: left; width: 200px;"></asp:Label><br />
<asp:RadioButtonList ID="rdlMinor" runat="server" RepeatDirection="Horizontal" Width="130px" Style="float: left;" BorderStyle="None" RepeatLayout="Flow" ()>
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem Selected="True">No</asp:ListItem>
</asp:RadioButtonList>
<asp:Label ID="lblJoint" CssClass="boldIt" runat="server" Text="Is this for a joint account?" Style="float: left; width: 200px;"></asp:Label><br />
<asp:RadioButtonList ID="rdlJoint" runat="server" RepeatDirection="Horizontal" Width="130px" Style="float: left;" BorderStyle="None" RepeatLayout="Flow">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem Selected="True">No</asp:ListItem>
</asp:RadioButtonList>
Run Code Online (Sandbox Code Playgroud) 假设这个页面的代码:
<h:form prependId="false" id="form">
<h:selectManyCheckbox id="checkBoxList" value="#{backedBean.lstIdSelectedItems}" layout="pageDirection">
<f:selectItems value="#{backedBean.lstAvailableItems}" var="item" itemLabel="#{item.label}" itemValue="#{item.value}" />
<f:ajax listener="#{backedBean.itemClicked}" />
</h:selectManyCheckbox>
</h:form>
Run Code Online (Sandbox Code Playgroud)
和会话管理bean的代码:
public class BackedBean implements Serializable {
private List<SelectItem> lstAvailableItems;
private List<Long> lstIdSelectedItems;
public BackedBean() {
lstAvailableItems = new ArrayList<SelectItem>();
lstIdSelectedItems = new ArrayList<Long>();
}
@PostConstruct
private void postConstruct(){
for (int i = 0; i < 10; i++) {
SelectItem item = new SelectItem(new Long(i), "CHKID " + i);
lstAvailableItems.add(item);
}
}
public void itemClicked(AjaxBehaviorEvent ae){
HtmlSelectManyCheckbox uiCmp = (HtmlSelectManyCheckbox)ae.getSource();
// …Run Code Online (Sandbox Code Playgroud) 我在这里复制了我的问题小提琴:http://jsfiddle.net/U3pVM/2840/
正如标题所示,我无法设置使用ng-options填充的选择的选定值.
我搜索并尝试了所有可能的解决方案.任何帮助或建议将不胜感激!
HTML
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<select ng-model="ddlRooms" ng-options="r.id as r.Name for r in Rooms">
</select>
{{$scope.test}}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
角
function TodoCtrl($scope) {
$scope.Rooms = [{ Name: 'Toddler', id: 1 },{ Name: 'other', id: 2 }];
$scope.options = [{ Name: 'other', id: 2 }];
$scope.ddlRooms = $scope.options[0];
$scope.test = 'bla';
}
Run Code Online (Sandbox Code Playgroud)