ContentTemplate和多视图依赖属性

时间:2018-02-05 23:40:48

标签: c# wpf mvvm datatemplate contentcontrol

我有一个显示自定义user controlcontent controltextbox的窗口...当用户在自定义{{1}上选择radio button时,依赖属性更改,这反过来会将视图更改为数据模板

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

根据该值,我想在IsSearching完成之前禁用某些功能。我尝试过几种方式设置IsSearching文本,但无法找到检索绑定的正确方法。

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

textbox

的DataTemplates

<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>

2 个答案:

答案 0 :(得分:0)

由于您绑定到CTALightViewModel,您是否在CTALightViewModel中有一个名为IsSearching的属性,您可以将其设置为绑定中的路径? e.g

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

如果没有,您可以在IsSearching中创建名为CTALightViewModel的媒体资源,在媒体资源的设置工具中,您可以调用OnPropertyChanged(),以便用户界面了解更改信息。发生。

答案 1 :(得分:0)

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

相关问题