【问题标题】:binding, to the viewmodel, of a control located in the template of an ItemsControl将位于 ItemsControl 模板中的控件绑定到视图模型
【发布时间】:2018-04-17 12:33:30
【问题描述】:

是的,

也许这是一个简单的问题,但我被困在这个问题上,所以我需要你的帮助。

我有一个 TextBox、一个 DataTemplate、一个 ItemsControl.ItemTemplate、一个 ItemsControl,我想在我的 ViewModel 的一个属性中发送键入的值。

ItemsControl 
|ItemsControl.ItemTemplate 
||Datatemplate 
|||Grid 
||||textBox --> Value == > MyViewModel

目前,我有一个“DisplayedItems”列表,绑定到填充 ItemsControl 的 ItemsControl 的“ItemsSource”属性,并且我得到的值由列表中的 DisplayedItem 之一接收,不幸的是留在了那里.. . 而且我完全不知道如何从 ViewModel 类中获取或使用它。

||||textBox --> Value --> DisplayedItem --?-->ViewModelClass

<ItemsControl Name="IcChoice" ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Width="350" Height="150" >
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <Grid Visibility="{Binding Visible, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="*"/>

                    </Grid.RowDefinitions>
                    <TextBlock Text="{Binding Title}" Grid.Row="0"/>
                    <ComboBox ItemsSource="{Binding Children}" Visibility="{Binding ComboBoxVisibility}" Grid.Row="1"/>
                    <TextBox Text="{Binding HelloWorldViewModel.ReceivedText, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Visibility="{Binding TextBoxVisibility}" Grid.Row="1">

                    </TextBox>
                </Grid>
            </DataTemplate>

        </ItemsControl.ItemTemplate>
    </ItemsControl>

我能得到一些帮助吗?

【问题讨论】:

  • 您的意思是所有项目都绑定到窗口视图模型中的一个属性?那将是一个相对源绑定,但它似乎有点奇怪。还是您的意思是您想要集合中特定项目的价值。在这种情况下,可能会让您的 itenscontrol 成为列表框并绑定选定项。
  • 好吧,如果 ItemsSource="{Binding Items 是 DisplayedItem 的视图模型集合,则更改为

标签: wpf data-binding datatemplate


【解决方案1】:

我找到了解决办法。

我需要为我的封装控件定义一个特定的数据上下文:

  <TextBox   
                        Text="{Binding DataContext.ReceivedText, 
                        ElementName=Hello,  
                        UpdateSourceTrigger=PropertyChanged, 
                        Mode=TwoWay}" 
                        Visibility="{Binding TextBoxVisibility}" 
                        Grid.Row="1"/>

如果“Hello”是我的视图的名称(它与 viewModel 建立链接)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多