我试图将几个ListBox绑定到List.当一个表单上的ListBox更新时,我希望它也更新另一个表单框.
我遇到的问题是,当我更新基础List时,它似乎没有更新ListBox上的视图.如果我在调试中查看ListBox.Items,我可以看到我添加的所有项目都在那里,但是没有显示.此外,当我打开另一个在ListBox上显示List的表单时,它会正确显示已添加的任何项目.
private List<String> _list;
public Form1()
{
InitializeComponent();
_list = StaticInstanceOfList.GetInstance();
listbox1.DataSource = _list;
}
public void AddStringToList(string value)
{
if (!_list.Contains(value))
{
_list.Add(value);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个ListBox,当该集合中的任何内容发生更改时,它会更新ObservableCollection的内容,因此这是我为此编写的代码:
XAML:
<ListBox x:Name="UserListTest" Height="300" Width="200" ItemsSource="listOfUsers">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding LastName}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
C#:
public ObservableCollection<User> listOfUsers
{
get { return (ObservableCollection<User>)GetValue(listOfUsersProperty); }
set { SetValue(listOfUsersProperty, value); }
}
public static readonly DependencyProperty listOfUsersProperty =
DependencyProperty.Register("listOfUsers", typeof(ObservableCollection<User>), typeof(MainPage), null);
Run Code Online (Sandbox Code Playgroud)
我设置了一个对填充listOfUsers的WCF服务的调用:
void repoService_FindAllUsersCompleted(object sender, FindAllUsersCompletedEventArgs e)
{
this.listOfUsers = new ObservableCollection<User>();
foreach (User u in e.Result)
{
listOfUsers.Add(u);
}
//Making sure it got populated
foreach (User u in listOfUsers)
{
MessageBox.Show(u.LastName);
}
}
Run Code Online (Sandbox Code Playgroud)
ListBox永远不会填充任何内容.我认为我的问题可能在于xaml,因为ObservableCollection实际上包含了我的所有用户.
我已经研究了一段时间并且无济于事(我已经尝试过让命令工作了但是有一些我认为我很想念的东西.对于WPF做事的方式很新,但没有帮助(但是真的喜欢我用WPF找到的东西)
我遇到与此帖相同的问题: 从DataTemplate上的按钮获取ListBox行对象
我有一个列表框,每个数据模板中有标签+ 2个按钮.单击按钮时,我不需要知道单击了哪一行.(根本没有选择该行,因此Selectedindex不在问题中).我真的很喜欢Vaccano的方法,但似乎无法让它在他给出的信息上工作(他说自己非常简短)
<ListBox x:Name="lbDirectoryTreeBox" Height="auto" Width="auto" Grid.Column="0" Grid.Row="0" Margin="10,10,5,10" ItemsSource="{Binding}"
MouseDoubleClick="lbDirectoryBox_MouseDoubleClick" SelectionChanged="lbDirectoryTreeBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Button x:Name="btnOpenFolder" Content="R" Width="20" Height="20" Click="btnDirectoryOpen_Click" />
<Button x:Name="btnAddFolder" Content="R" Width="20" Height="20" Click="btnAddFolder_Click" />
<Label Width="auto" Content="{Binding Path=Name}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Run Code Online (Sandbox Code Playgroud)
单击任一按钮后,如何获取行的selectedindex?(在c#中)
在此提前感谢您的帮助.
我想将Silverlight ListBox绑定到Dictionary<int, string>.我试过以下没有成功:
someListBox.ItemsSource = someItems;
Run Code Online (Sandbox Code Playgroud)
和
someListBox.ItemsSource = someItems.Values;
Run Code Online (Sandbox Code Playgroud) 我正在使用列表框来通过文本框获取用户的输入.如果用户通过文本框提供输入,我可以解析列表框中的值并执行操作.现在如果用户没有给出任何输入,那么listbox不包含任何内容.我想如果列表框为空它应该在msg框中显示错误..我正在尝试我的代码,但它不工作..
string text1 = lstboxbulkto.ToString();
if (lstboxbulkto.ToString().Equals(null))
{
MessageBox.Show("hiii");
}
Run Code Online (Sandbox Code Playgroud)
其中lstboxbulkto是列表框的名称.如何才能做到这一点.
我有一个richTextBox1与此行:
my test
my test2
Run Code Online (Sandbox Code Playgroud)
并尝试使用此代码将行插入到列表框或combox中:
richTextBox1.Text = File.ReadAllText(@"New ID.txt").ToString();
listBox1.Items.Add(richTextBox1.Text);
Run Code Online (Sandbox Code Playgroud)
但显示列表框
mytestmytest2
如何将每个项目插入(附加)为新行?
我想使用MVVM模式在多个数据绑定的列表框上实现拖放.我不是想在列表框之间拖放,而是希望用户能够在每个列表框中拖放listboxitems,以便他们可以重新排列排序顺序.我在SO上发现这篇帖子非常有帮助:
我想尝试使这些方法更"通用",以便它可以在任何绑定到不同类型的Observable Collections的列表框上工作.所以说这是我在VIEW中的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">
<Window.Resources>
<Style TargetType="{x:Type ListBoxItem}" x:Key="ListBoxItemDragDrop">
<Setter Property="AllowDrop" Value="True" />
<EventSetter Event="PreviewMouseMove" Handler="ListBoxItem_PreviewMouseMoveEvent" />
<EventSetter Event="Drop" Handler="listbox1_Drop" />
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<ListBox Name="listbox1"
ItemsSource="{Binding OCofType1}"
ItemContainerStyle="{StaticResource ListBoxItemDragDrop}" />
<ListBox Name="listbox2" Grid.Column="1"
ItemsSource="{Binding OCofType2}"
ItemContainerStyle="{StaticResource ListBoxItemDragDrop}"/>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
OC绑定的位置是ObservalbeCollection <Type1>和ObservalbeCollection <Type2>.这是VIEW中抓取鼠标移动事件并检查它是否是拖动的方法:
void ListBoxItem_PreviewMouseMoveEvent(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed && sender is ListBoxItem)
{
ListBoxItem draggedItem = (ListBoxItem)sender;
DragDrop.DoDragDrop(draggedItem, draggedItem.DataContext, DragDropEffects.Move);
draggedItem.IsSelected …Run Code Online (Sandbox Code Playgroud) 我正在努力学习德尔福,而我正在制作游戏.我曾经知道一点Pascal,但我对Delphi一无所知.几年前我在Pascal制作了这款游戏.它包含这样一行:
writeln(turn,' ',input,' ',die[turn],' ',wou[turn]);
Run Code Online (Sandbox Code Playgroud)
基本上它意味着显示用户输入的计算结果,这些数字之间有一些空格(所有这些变量都是除"输入"之外的数字,这是一个字符串).
我试图在Delphi中以类似方式显示结果.虽然最好使用表格,但我不知道如何使用表格,所以我尝试使用列表框.但是items.add程序不像Pascal那样工作writeln,因此我现在卡住了.
我是一个新的,初学者,所以请让我容易理解.
我正在尝试使用vba在excel上做一个小的“另存为”界面,以便可以将自己放入工作表的一些信息保存在另一工作表中。我差不多完成了,它看起来像这样:单击此处查看我的“另存为”界面
我的问题是我想双击列表中的一个元素(例如SoCs2)来创建一个事件。双击时,我希望字符串“ SoCs2”出现在下面的文本框中。
我尝试过这样的事情:
Private Sub Listbox1_BeforeDoubleClick(ByVal Cancel As MSForms.ReturnBoolean)
With Me.ListBox1
For i = 0 To .ListCount - 1
If .Selected(i) Then
Me.TextBox1.Value = .List(i, 0)
Exit For
End If
Next
End With
End Sub
Run Code Online (Sandbox Code Playgroud)
我的列表框称为ListBox1,而我的文本框称为TextBox1。不幸的是,此代码不起作用:当我双击ListBox的一项时,它绝对不起作用。有人可以帮我解决这个问题吗?
我正在使用WPF(C#,Visual Studio 2010,MVVM Light)并且遇到三个Listbox控件的问题.
所以情况就是这样:我有三个Listbox.第一个是"示例"的ID列表.第二个也是"相关项目"的ID列表.它们是我的程序中的示例和相关项目,它们代表动词(如语言类型).
如果单击这两个ListBox中的任何一个,它将填充第三个ListBox.两者互不影响,只有第三种.第三个不影响其他两个.第三个一次只能容纳一个项目.
除了一种情况外,它运作良好.假设'examples'ListBox包含ID 100002和100003.我们还说'相关项'ListBox包含ID 100004和100005.我单击100002,因此第一个ListBox的选定项成为该项.第三个列表框显示100002的详细信息(应该如此).然后我点击100004上的第二个列表框.这成为第二个列表框的选定项目,第三个列表框显示100004.到目前为止.但现在我想说无论出于什么原因我想再回到100002.由于它仍然是第一个列表框中的选定项目,因此再次单击它无效.视图模型中的setter甚至没有被触发,因此我无法在那里做任何事情.事实上,我不确定在这种情况下我的选择是什么.我确实考虑过使用'examples'的setter来设置'相关项'的选定项为null,反之亦然,但我意识到这将导致无限循环.
我也尝试使用一个变量链接到前两个ListBoxes的选定项目,但这并没有解决问题.
我也尝试过以下方法:
private LanguageItemViewModel _selectedExampleOrRelatedItemTestVM = null;
public LanguageItemViewModel SelectedExampleOrRelatedItemTestVM
{
get { return _selectedExampleOrRelatedItemTestVM; }
set
{
if (_selectedExampleOrRelatedItemTestVM != value)
{
Mediator.EventMediator.Instance.PassLanguageItemAsExampleOrRelatedItem(value);
_selectedExampleOrRelatedItemTestVM = null;
RaisePropertyChanged("SelectedExampleOrRelatedItemTestVM");
}
}
}
Run Code Online (Sandbox Code Playgroud)
因此,在触发相关事件之后,变量本身只是设置为null.这也行不通.
也许有人在那里有一些建议,甚至一些我没有考虑过的途径?谢谢.
listbox ×10
c# ×7
wpf ×3
binding ×2
silverlight ×2
winforms ×2
.net ×1
combobox ×1
datasource ×1
datatemplate ×1
delphi ×1
excel ×1
forms ×1
mvvm ×1
vba ×1