wpf - Dependecy 属性用作源但不用作目标
问题描述
我在用户控件中有一个依赖属性。
public ComputerListView()
{
InitializeComponent();
vm = new ComputerViewModel();
this.DataContext = vm;
DispatcherTimer time = new DispatcherTimer(TimeSpan.FromSeconds(1), DispatcherPriority.Normal,
delegate
{
SetValue(AutoCounterProperty, vm.SimpleCounter);
},
Dispatcher);
}
public int AutoCounter
{
get { return (int)GetValue(AutoCounterProperty); }
set { SetValue(AutoCounterProperty, value); }
}
// Using a DependencyProperty as the backing store for Counter. This enables animation, styling, binding, etc...
public static readonly DependencyProperty AutoCounterProperty =
DependencyProperty.Register("AutoCounter", typeof(int), typeof(ComputerListView), new PropertyMetadata(0));
}
<StackPanel>
<local:ComputerListView x:Name="computerListView" AutoCounter="{Binding VMCounter, Mode=TwoWay}"/>
<TextBox Background="Beige" Text="{Binding VMCounter, Mode=TwoWay}"/>
<TextBlock Background="LightCyan" Text="{Binding ElementName=computerListView, Path=AutoCounter}"/>
<Button Content="VMCounter" Click="VMCounter_Button_Click"/>
</StackPanel>
我希望该VMCounter
属性将得到更新,因为AutoCounter
它将在依赖属性中运行,并且 TextBox 将从该VMCounter
属性中更新,但它显示为 0,永远不会更新。
我已经使用了AutoCounter
我在 TextBlock 中的方式,然后它更新得很好。
我认为 DP 的主要用途是能够像第一种情况一样用作目标属性。为什么它不能那样工作?
解决方案
推荐阅读
- javascript - React.js 如何在组件内循环和渲染组件?
- php - 如何使 mail() 消息间距
- spring-boot - 无法让 liquibase 读取我的 JPA 实体的更改
- powershell - 我如何用 powershell 编写优雅的 ping
- javascript - Toogle Google 输入工具通过 Angular
- meilisearch - MeiliSearch 自动补全
- python - 如何使用 Selenium 在菜单中选择一个选项?
- firebase - CocoaPods 找不到 pod“RNFBApp”的兼容版本
- c# - 无法使用 masstransit 将消息发布到 rabbitmq
- python - 将成对并联而不是串联