相关疑难解决方法(0)

绑定到DataConmplate for ItemsControl中的自定义控件

我有绑定的问题DataTemplate基础上定义DataTypeItemsControl,当我想结合我的自定义用户控件.

出于演示目的,我创建了简单的Item类示例,其中我有这样的项集合:

public class Item
{
    public string ItemNameToBeSureWhatPropertyIsBound { get; set; } 
}
Run Code Online (Sandbox Code Playgroud)

在我的ViewModel中,我创建了这样的集合,并将其公开(有一个项目可以单独进行比较):

public class MainWindowViewModel : INotifyPropertyChanged
{
    private ObservableCollection<Item> _items;
    private Item _exampleItem;

    public MainWindowViewModel()
    {
        Items = new ObservableCollection<Item>(new[] { new Item { ItemNameToBeSureWhatPropertyIsBound = "Me" }, new Item { ItemNameToBeSureWhatPropertyIsBound = "MySelf" }, new Item { ItemNameToBeSureWhatPropertyIsBound = "Ich" }, });
        ExampleItem = Items.LastOrDefault();
    }

    public ObservableCollection<Item> Items
    {
        get { return …
Run Code Online (Sandbox Code Playgroud)

c# data-binding wpf custom-controls itemscontrol

6
推荐指数
1
解决办法
6325
查看次数

MVVM 将嵌套子视图连接到子视图模型

我正在尝试在使用嵌套视图的已经工作的应用程序中建立嵌套 ViewModel。这是我想做的一个例子:

主窗口视图:

<Window x:Name="FCTWindow" x:Class="CatalogInterface.MainWindow"
        xmlns:local="clr-namespace:CatalogInterface"
        xmlns:vm="clr-namespace:CatalogInterface.ViewModels"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="532">

    <Window.Resources>
        <vm:MainWindowViewModel x:Key="ViewModel" />
    </Window.Resources>

    <Grid DataContext="{Binding Path=ViewModel.DirFilesListBoxViewModel}" x:Name="BodyGridLeft" Grid.Row="0" Grid.Column="0">
        <local:ctlDirFilesListBox>
            <!--
                Need to access the `ItemsSource="{Binding }"` and
                 `SelectedItem="{Binding Path=}"` of the ListBox in 
                 `ctlDirFilesListBox` view -->
        </local:ctlDirFilesListBox>
</Window>
Run Code Online (Sandbox Code Playgroud)

子视图:

<UserControl x:Class="CatalogInterface.ctlDirFilesListBox"
         xmlns:local="clr-namespace:CatalogInterface"
         xmlns:vm="clr-namespace:CatalogInterface.ViewModels"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">

<Grid x:Name="MainControlGrid">           
    <ListBox SelectionChanged="ListBoxItem_SelectionChanged" 
                         HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Background="#FFFFFF"
                         Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" BorderThickness="0">
        <ListBox.ItemContainerStyle>
            <Style TargetType="{x:Type ListBoxItem}" BasedOn="{StaticResource {x:Type ListBoxItem}}">
                <EventSetter Event="MouseDoubleClick" Handler="ListBoxItem_MouseDoubleClick"/>
                <EventSetter Event="KeyDown" Handler="ListBoxItem_KeyDown"/>
            </Style>
        </ListBox.ItemContainerStyle>
    </ListBox>
</Grid>
</UserControl> …
Run Code Online (Sandbox Code Playgroud)

c# wpf nested mvvm

1
推荐指数
1
解决办法
3127
查看次数

标签 统计

c# ×2

wpf ×2

custom-controls ×1

data-binding ×1

itemscontrol ×1

mvvm ×1

nested ×1