ContentTemplate 和多视图依赖属性
ContentTemplate and Multiple View Dependency Properties
我有一个显示自定义 user control
、content control
和 textbox
的 window...当用户选择 radio button
在自定义 user control
上,依赖项 属性 发生变化,这又将视图更改为来自数据模板
我遇到的问题:我无法正确检索在交换 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}" />
由于您要绑定到 CTALightViewModel
,因此 CTALightViewModel
中是否有一个名为 IsSearching
的 属性 可以设置为绑定中的路径?例如
<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />
如果没有,您可以在 CTALightViewModel
中创建名为 IsSearching
的 属性,而在 属性 的 setter 中,您可以调用您的 OnPropertyChanged()
以便 UI 随时了解变化。
我意识到我需要实现一个视图模型库来实现这一点。我创建了一个并让其他视图模型继承自该基础。
我有一个显示自定义 user control
、content control
和 textbox
的 window...当用户选择 radio button
在自定义 user control
上,依赖项 属性 发生变化,这又将视图更改为来自数据模板
我遇到的问题:我无法正确检索在交换 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}" />
由于您要绑定到 CTALightViewModel
,因此 CTALightViewModel
中是否有一个名为 IsSearching
的 属性 可以设置为绑定中的路径?例如
<TextBox Text="{Binding ElementName=ViewSwap, Path=?????, Mode=OneWay}" />
如果没有,您可以在 CTALightViewModel
中创建名为 IsSearching
的 属性,而在 属性 的 setter 中,您可以调用您的 OnPropertyChanged()
以便 UI 随时了解变化。
我意识到我需要实现一个视图模型库来实现这一点。我创建了一个并让其他视图模型继承自该基础。