附带的屏幕截图来自OS X/Firefox 3.请注意,中心选项卡(图像)周围有一条虚线,显然是因为它是最近选择的选项卡.有没有办法可以消除CSS或JavaScript中的虚线?(嗯......免费的图像托管服务减少了图像的大小.但是如果你能看到它,你会注意到块周围的虚线选择区域.)
屏幕截图http://www.freeimagehosting.net/uploads/th.fadf78173b.png
在安装了Net Framework 3.5 sp1的wpf应用程序中设置SelectedItem programmaticaly时,我感到很困惑.我仔细阅读了大约一百个帖子\主题但仍然困惑((我的xaml:
<ComboBox name="cbTheme">
<ComboBoxItem>Sunrise theme</ComboBoxItem>
<ComboBoxItem>Sunset theme</ComboBoxItem>
</ComboBox>
Run Code Online (Sandbox Code Playgroud)
如果我在其中一个项目中添加IsSelected ="True"属性 - 它不会设置此项目.为什么?而我在代码中尝试不同,仍然无法设置所选项目:
cbTheme.SelectedItem=cbTheme.Items.GetItemAt(1); //dosn't work
cbTheme.Text = "Sunrise theme"; //dosn't work
cbTheme.Text = cbTheme.Items.GetItemAt(1).ToString();//dosn't work
cbTheme.SelectedValue = ...//dosn't work
cbTheme.SelectedValuePath = .. //dosn't work
//and even this dosn't work:
ComboBoxItem selcbi = (ComboBoxItem)cbTheme.Items.GetItemAt(1);//or selcbi = new ComboBoxItem
cbTheme.SelectedItem = selcbi;
Run Code Online (Sandbox Code Playgroud)
SelectedItem不是readonly属性,为什么它不工作?我认为这应该是微软的问题,而不是我的问题.或者我错过了什么?我尝试使用ListBox,并且所有工作都可以正常使用相同的代码,我可以设置选择,获取选择等等......那么我可以用ComboBox做什么?也许一些技巧???
似乎没有人找到一种方法来将组合框设置为SelectedItem ="Binding Property".
解决方案是在组合框项目源中的ViewModel对象中使用IsSelected属性吗?
我有一个ComboBox,它的ItemsSource绑定到一个静态List<CustomSettings>的选项.ComboBox是绑定到CustomObject类的表单的一部分,该类的一个属性是CustomSettingProperty.
我想将ComboBox的SelectedItem绑定到CustomObject中指定的属性,但是SelectedItem="{Binding Path=CustomSettingProperty}"没有设置默认的选定项.使用断点我可以看到它正在调用get;方法,所以我认为问题可能在于CustomSettingProperty是单独创建的,List<CustomObject>因此WPF认为它不是同一个项目.
是否有捷径可寻?或者也许是另一种选择,因为CustomSettings类确实包含Id?
我想知道如何在选择列表框的项目时添加样式.我有以下列表框:
<ListBox x:Name="ListBoxDays"
VerticalAlignment="Top"
ItemTemplate="{StaticResource WeekDayTemplate}"
ItemsSource="{Binding WeekDayList}" />
Run Code Online (Sandbox Code Playgroud)
我还有一个DataTemplate到列表框.
<phone:PhoneApplicationPage.Resources>
<DataTemplate x:Key="WeekDayTemplate">
<StackPanel x:Name="stackPanel" Orientation="Horizontal" Width="400" Margin="12,0,0,10" Height="100" >
<StackPanel VerticalAlignment="Center" Orientation="Vertical">
<TextBlock Text="{Binding WeekDayName}" Style="{StaticResource PhoneTextExtraLargeStyle}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="{StaticResource PhoneRadioCheckBoxPressedBorderBrush}" UseLayoutRounding="True" />
<TextBlock Text="{Binding ShortDate}" Style="{StaticResource PhoneTextTitle2Style}" TextWrapping="Wrap" TextTrimming="WordEllipsis" Foreground="{StaticResource PhoneBorderBrush}" Margin="25,0,12,0" />
</StackPanel>
</StackPanel>
</DataTemplate>
</phone:PhoneApplicationPage.Resources>
Run Code Online (Sandbox Code Playgroud)
在我选择列表框中的项目时,不会发生颜色更改.
在下面的代码中,当用户在组合框中选择Customer时,客户的名称将显示在文本框中.我在ViewModel上填充了一个带有ObservableCollection属性的Combox框,但是如何在ViewModel中处理SelectedItem事件?
使用代码隐藏实现这一点很容易,如下所示,但是如何使用MVVM模式执行此操作?
我目前在我可以使用的基本MVVM模板中有DelegateCommand和AttachedBehaviors,但是当"combobox选择一个新项目"时,我无法弄清楚如何让它们触发.
视图:
<Window.Resources>
<DataTemplate x:Key="CustomerTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<DockPanel LastChildFill="False" Margin="10">
<ComboBox
x:Name="CustomerList"
ItemTemplate="{StaticResource CustomerTemplate}"
HorizontalAlignment="Left"
DockPanel.Dock="Top"
Width="200"
SelectionChanged="CustomerSelected"
ItemsSource="{Binding Customers}"/>
<TextBlock x:Name="CurrentlySelectedCustomer"/>
</DockPanel>
Run Code Online (Sandbox Code Playgroud)
代码背后:
private void CustomerSelected(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
Customer customer = (Customer)CustomerList.SelectedItem;
CurrentlySelectedCustomer.Text = String.Format("{0} {1}", customer.FirstName, customer.LastName);
}
Run Code Online (Sandbox Code Playgroud) 我有一个WPF TabControl,TabItem标题中有几个按钮.我想在单击一个带标题的按钮时更改所选的选项卡.这是一段代码:
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<TabControl>
<TabItem Content="Item 1 Content">
<TabItem.Header>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="14" FontWeight="Bold" Text="Item1"/>
<StackPanel Orientation="Horizontal">
<Button Content="Action 1"/>
<Button Content="Action 2"/>
</StackPanel>
</StackPanel>
</TabItem.Header>
</TabItem>
<TabItem Content="Item 2 Content">
<TabItem.Header>
<StackPanel Orientation="Vertical">
<TextBlock FontSize="14" FontWeight="Bold" Text="Item2"/>
<StackPanel Orientation="Horizontal">
<Button Content="Action 1"/>
<Button Content="Action 2"/>
</StackPanel>
</StackPanel>
</TabItem.Header>
</TabItem>
</TabControl>
</Grid>
</Page>
Run Code Online (Sandbox Code Playgroud)
此示例显示了几个Tabs.如果单击标题背景,则选择选项卡,但是,如果单击按钮,则不会选择选项卡.我希望按钮接受点击,但我也希望选中与该按钮对应的选项卡.有人可以帮忙吗?
谢谢,Hitesh
我有一个视图模型来管理对话框类型的视图,允许过滤列表(如果需要)和选择项目.无论我是否将IsSynchronizedWithCurrentItem设置为true,代码都能正常工作.我的理解是这个属性在ListView中默认是不正确的,所以我想更好地理解这个属性.
这是视图的xaml中的绑定设置(在没有synch属性设置的情况下也可以正常工作):
<ListView
ItemsSource="{Binding Projects.View}"
IsSynchronizedWithCurrentItem="True"
SelectedItem="{Binding SelectedProject, Mode=TwoWay}"
>
Run Code Online (Sandbox Code Playgroud)
视图模型Projects实际上是一个由私有ObservableCollection支持的CollectionViewSource.我想我从Josh Smith的一个示例项目中琢磨了这个想法,但老实说我现在不记得了.以下是与xaml绑定相关的VM的相关部分:
private ObservableCollection<ProjectViewModel> _projectsInternal { get; set; }
public CollectionViewSource Projects { get; set; }
private void _populateProjectListings(IEnumerable<Project> openProjects) {
var listing = (from p in openProjects
orderby p.Code.ToString()
select new ProjectViewModel(p)).ToList();
foreach (var pvm in listing)
pvm.PropertyChanged += _onProjectViewModelPropertyChanged;
_projectsInternal = new ObservableCollection<ProjectViewModel>(listing);
Projects = new CollectionViewSource {Source = _projectsInternal};
}
/// <summary>Property that is updated via the binding to the view</summary>
public ProjectViewModel SelectedProject { get; set; …Run Code Online (Sandbox Code Playgroud) 我想将组合框中的值作为参数传递给SQL语句.Winforms组合框为我提供了几个检索值的选项,即SelectedItem,SelectedText和SelectedValue.在这种情况下哪一个最好/最安全?
我的问题基本上就是这个问题.我认为这将有助于提供一些更容易重现问题的信息和代码.
使用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) selecteditem ×10
wpf ×7
combobox ×4
mvvm ×3
data-binding ×2
itemssource ×2
binding ×1
css ×1
listbox ×1
selectedtext ×1
set ×1
tabcontrol ×1
winforms ×1