ContentTemplate和多视图依赖属性

问题描述 投票:0回答:2

我有一个窗口显示自定义user controlcontent controltextbox ...当用户在自定义radio button上选择user control时,依赖项属性会更改,从而将视图更改为来自数据模板

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

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

我还尝试将依赖属性绑定到mainviewmodel(CTALightViewModel)上的属性,但它似乎没有正常工作。绝对有点失落,所以任何帮助都表示赞赏。

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

的DataTemplates

<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

<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />
c# wpf mvvm datatemplate contentcontrol
2个回答
0
投票

既然你绑定了CTALightViewModel,你在CTALightViewModel中有一个名为IsSearching的属性,你可以设置为绑定中的路径吗?例如

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

如果没有,你可以在你的IsSearching中创建一个名为CTALightViewModel的属性,在属性的setter中,你可以调用你的OnPropertyChanged(),这样UI就会在发生变化时了解变化。


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.