【问题标题】:ContentTemplate and Multiple View Dependency PropertiesContentTemplate 和多视图依赖属性
【发布时间】:2018-02-05 23:40:47
【问题描述】:

我有一个显示自定义user controlcontent controltextbox...将视图更改为来自数据模板

我遇到的问题:我无法正确检索在交换的user control 上公开的基础依赖项属性。例如,每个用户控件都公开了一个IsSearching 依赖属性。

基于该值,我想禁用某些功能,直到 IsSearching 完成。我尝试了几种方法设置textbox 文本,但找不到检索绑定的正确方法。

我还尝试将依赖属性绑定到主视图模型 (CTALightViewModel) 上的属性,但它似乎无法正常工作。在这方面肯定有点迷失,所以任何帮助表示赞赏。

<views:CTAAddress x:Name="CTAAddressView" IsSearching="{Binding VMBusy, Mode=OneWay}"/>

数据模板

<Window.Resources>
    <DataTemplate x:Key="AddressTemplate" DataType="{x:Type viewmodel:CTAAddressViewModel}">
        <views:CTAAddress />
    </DataTemplate>
    <DataTemplate x:Key="PremiseTemplate" DataType="{x:Type viewmodel:CTAPremiseViewModel}">
        <views:CTAPremise  />
    </DataTemplate>
</Window.Resources>
<Window.DataContext>
    <viewmodel:CTALightViewModel />
</Window.DataContext>

内容控制

<ContentControl x:Name="ViewSwap" Content="{Binding }">
    <ContentControl.Style>
       <Style TargetType="{x:Type ContentControl}">
           <Style.Triggers>
              <DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="0">
                   <Setter Property="ContentTemplate" Value="{StaticResource AddressTemplate}" />
              </DataTrigger>
              <DataTrigger Binding="{Binding ElementName=SearchOptions, Path=IsSelected}" Value="1">
                  <Setter Property="ContentTemplate" Value="{StaticResource PremiseTemplate}" />
              </DataTrigger>                  
          </Style.Triggers>
       </Style>
   </ContentControl.Style>
</ContentControl>

要显示的文本框

<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />

【问题讨论】:

    标签: c# wpf mvvm datatemplate contentcontrol


    【解决方案1】:

    由于您绑定到CTALightViewModel,您在CTALightViewModel 中有一个名为IsSearching 的属性,您可以将其设置为绑定中的路径吗?例如

    <TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" /> 
    

    如果没有,您可以在 CTALightViewModel 中创建名为 IsSearching 的属性,并在属性的设置器中调用 OnPropertyChanged(),以便 UI 在发生更改时了解更改。

    【讨论】:

    • 我确实有一个名为VMBusy 的属性,它实现了CTALightViewModel 中的OnPropertyChanged()。我只是不确定放置绑定的部分。例如,我是将它绑定到每个data template 和相应的依赖属性,还是尝试从content control 元素本身获取它。这两种方法我都试过了,但都没有奏效。
    【解决方案2】:

    我意识到我需要实现一个视图模型库来实现这一点。我创建了一个并让其他视图模型从这个基础继承。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-21
      • 2015-11-26
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2014-03-01
      • 1970-01-01
      相关资源
      最近更新 更多