我ListBox在我的wpf窗口中绑定了一个ObervableCollection.如果有人点击了某个元素ListBox(就像链接一样),我想打开浏览器.谁能告诉我怎么做?我发现了listboxviews的一些东西,它只是以这种方式工作还是有一种方法只是使用ListBox?
你的
塞巴斯蒂安
在我的申请中,我有一个ListBox项目.该应用程序是用WPF编写的.
如何自动滚动到最后添加的项目?我希望在ScrollViewer添加新项目时将其移动到列表的末尾.
有什么事ItemsChanged吗?(我不想使用这个SelectionChanged活动)
这是我的情况:
<ListBox ItemsSource="{Binding Path=AvailableUsers}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Id}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Command="{Binding Path=Load}" CommandParameter={???? What goes here ????}/>
Run Code Online (Sandbox Code Playgroud)
我想要的是传递ListBox中当前选中的Id.我有幕后的viewmodel,基本上是这样的:
public class ViewModel : DependencyObject
{
ICommand Load { get; set; }
// dependency property but I didn't bother to write it out like one
List<User> AvailableUsers { get; set}
}
Run Code Online (Sandbox Code Playgroud)
如何使用xaml发送当前选定的项目?
我需要在a中对字符串进行排序ListBox,但它通过另一个组件绑定到视图模型DataContext.所以我不能直接在XAML中实例化视图模型,就像在这个例子中一样,它使用了ObjectDataProvider.
在我的XAML中:
<ListBox ItemsSource="{Binding CollectionOfStrings}" />
Run Code Online (Sandbox Code Playgroud)
在我的视图模型中:
public ObservableCollection<string> CollectionOfStrings
{
get { return collectionOfStrings; }
}
Run Code Online (Sandbox Code Playgroud)
在另一个组件中:
view.DataContext = new ViewModel();
Run Code Online (Sandbox Code Playgroud)
没有代码背后!所以使用纯XAML,我如何对ListBox中的项进行排序?同样,XAML不拥有视图模型的实例化.
我想找到一种更好的方法来填充c#中的checkedlistbox中的通用列表.
我可以很容易地做到以下几点:
List<string> selectedFields = new List<string>();
foreach (object a in chkDFMFieldList.CheckedItems) {
selectedFields.Add(a.ToString());
}
Run Code Online (Sandbox Code Playgroud)
必须有一个更精细的方法将CheckedItems集合强制转换为我的列表.
这是一个简单的过程吗?
我只是为内部工具编写一个快速的hacky UI.
我不想花一个年龄.
我上了课FruitViewModel.它描述了ListBox项目的ViewModels .
<ListBox ItemsSource="{Binding Fruits}">
Run Code Online (Sandbox Code Playgroud)
而且我有
class BananaViewModel : FruitViewModel
Run Code Online (Sandbox Code Playgroud)
和
class AppleViewModel : FruitViewModel
Run Code Online (Sandbox Code Playgroud)
Fruits包含绑定的BananaViewModels和AppleViewModels ItemsSource.
如何为苹果和香蕉制作不同的模板?它们应该在一个列表中,但具有不同的模板
我有一个ListBox控件,我在网格布局中呈现固定数量的ListBoxItem对象.所以我将ItemsPanelTemplate设置为Grid.
我从后面的代码访问Grid以配置RowDefinitions和ColumnDefinitions.
到目前为止,它都像我期望的那样工作.我有一些自定义的IValueConverter实现,用于返回每个ListBoxItem应该出现的Grid.Row和Grid.Column.
但是我有时会遇到奇怪的绑定错误,我无法弄清楚它们为什么会发生,或者即使它们在我的代码中.
这是我得到的错误:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ItemsControl', AncestorLevel='1''. BindingExpression:Path=HorizontalContentAlignment; DataItem=null; target element is 'ListBoxItem' (Name=''); target property is 'HorizontalContentAlignment' (type 'HorizontalAlignment')
谁能解释一下发生了什么?
哦,这是我的XAML:
<UserControl.Resources>
<!-- Value Converters -->
<v:GridRowConverter x:Key="GridRowConverter" />
<v:GridColumnConverter x:Key="GridColumnConverter" />
<v:DevicePositionConverter x:Key="DevicePositionConverter" />
<v:DeviceBackgroundConverter x:Key="DeviceBackgroundConverter" />
<Style x:Key="DeviceContainerStyle" TargetType="{x:Type ListBoxItem}">
<Setter Property="FocusVisualStyle" Value="{x:Null}" />
<Setter Property="Background" Value="Transparent" />
<Setter Property="Grid.Row" Value="{Binding Path=DeviceId, Converter={StaticResource GridRowConverter}}" />
<Setter Property="Grid.Column" Value="{Binding Path=DeviceId, Converter={StaticResource GridColumnConverter}}" />
<Setter Property="Template"> …Run Code Online (Sandbox Code Playgroud) 是否可以使用ObjectDataProvider方法将ListBox绑定到枚举,并以某种方式设置样式以显示描述attriibute?如果是这样,怎么会这样做...?
我想改变一些行的背景颜色ListBox.我有两个列表,其中一个有名称,并显示在一个ListBox.第二个列表与第一个列表具有相似的值List.单击按钮时,我想搜索ListBox第二个List,然后更改ListBox出现在那些值中的颜色List.我的搜索结果ListBox如下:
for (int i = 0; i < listBox1.Items.Count; i++)
{
for (int j = 0; j < students.Count; j++)
{
if (listBox1.Items[i].ToString().Contains(students[j].ToString()))
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道使用哪种方法来改变ListBox行的外观.有谁能够帮我?
**编辑:**
我写了我的代码如下:
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
Brush myBrush = Brushes.Black;
Brush myBrush2 = Brushes.Red;
g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
for (int i = …Run Code Online (Sandbox Code Playgroud)