Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVVM如何将字符串的ObservableCollection绑定到ListBox WPF_C#_Wpf_Xaml_Mvvm_Listbox - Fatal编程技术网

C# MVVM如何将字符串的ObservableCollection绑定到ListBox WPF

C# MVVM如何将字符串的ObservableCollection绑定到ListBox WPF,c#,wpf,xaml,mvvm,listbox,C#,Wpf,Xaml,Mvvm,Listbox,通常,我将ObservableCollection绑定到自己的类。但在这种情况下,我需要使用MVVM将ListBox中字符串的ObservableCollection绑定到WPF中 我的xml是 <Window x:Class="ListBoxDynamic.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microso

通常,我将ObservableCollection绑定到自己的类。但在这种情况下,我需要使用MVVM将ListBox中字符串的ObservableCollection绑定到WPF中

我的xml是

<Window x:Class="ListBoxDynamic.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"
    xmlns:vm="clr-namespace:ListBoxDynamic.ViewModel">
<Grid Margin="0,0,-8,0">
    <ListBox Width="100" Height="90"  ItemsSource="{Binding ListOfItems}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <ToggleButton Command="{Binding SelectItemCommand}" CommandParameter="{Binding ElementName=Item}" >
                    <ToggleButton.Template>
                        <ControlTemplate TargetType="ToggleButton">
                            <TextBlock x:Name="Item" Text="{Binding What I must to write?}" />
                        </ControlTemplate>
                    </ToggleButton.Template>
                </ToggleButton>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

我的主视图模型

private ObservableCollection<string> _listOfItems = new ObservableCollection<string>();
    public ObservableCollection<string> ListOfItems
    {
        get { return _listOfItems; }
        set { _listOfItems = value; RaisePropertyChanged("ListOfItems"); }
    }

    public ICommand SelectItemCommand { get; private set; }
    int counter = 1;
    public MainViewModel()
    {
        ListOfItems.Add("Add new Item");
        SelectItemCommand = new RelayCommand<object>((x) => ExecuteSelectItemCommand(x));

    }
private observateCollection_listOfItems=new observateCollection();
公共可观察收集项目列表
{
获取{return\u listOfItems;}
设置{u listOfItems=value;RaisePropertyChanged(“listOfItems”);}
}
public ICommand SelectItemCommand{get;private set;}
int计数器=1;
公共主视图模型()
{
项目列表。添加(“添加新项目”);
SelectItemCommand=newrelayCommand((x)=>ExecuteSelectItemCommand(x));
}

但我不知道我必须将文本块绑定到ToggleButton中。

我注意到的问题很少

  • 该命令在视图模型中而不是在数据项中可用,因此使用相对源绑定到该命令
  • “{Binding}”可用于引用当前项,因此无需使用elementname语法
  • 然后,您可以使用content presenter使其更加灵活,而不是在切换按钮中放置文本框
这样就可以了

<ListBox Width="100"
         Height="90"
         ItemsSource="{Binding ListOfItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <ToggleButton Command="{Binding SelectItemCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ListBox}}"
                          CommandParameter="{Binding}"
                          Content="{Binding}">
                <ToggleButton.Template>
                    <ControlTemplate TargetType="ToggleButton">
                        <ContentPresenter />
                    </ControlTemplate>
                </ToggleButton.Template>
            </ToggleButton>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

由于支持每个
ListBoxItem
的数据是字符串,因此每个
ListBoxItem
DataContext
将是字符串


因此,执行
将向您显示文本块中支持listboxitem的字符串

将该字符串包装为类的属性?我不想这样做,难道不是唯一的字符串吗?我不想创建一个仅用于绑定属性的新类。只需编写
我尝试了这个nit,但(“添加新项”)元素没有出现。@ncampuzano我尝试了这个..工作正常。。您确定为视图设置了正确的datacontext吗?你能检查一下绑定错误吗。但是。。。我添加的第一个元素在运行我的应用程序时没有出现。这可能不是模板的问题,请尝试检查集合是否有值,并查看输出窗口中是否存在任何绑定错误。很棒的find@ncampuzano,希望此模板对您有用,这是足够灵活,以适应任何类型的内容提供切换行为,如图像按钮,绘图按钮等。嗨,@nit我有一些类似的问题,寻求一些assistance@ArijitMukherjee当然,让我知道在哪里?在这里吗?