【问题标题】:Set ViewModel property when a TextBox is focused当 TextBox 获得焦点时设置 ViewModel 属性
【发布时间】:2012-03-28 10:46:45
【问题描述】:

我有一个 MVVM 应用程序,我想跟踪我的 ViewModel 中的焦点元素。

我想在 TextBox 获得焦点时设置 ViewModel 属性。 我想要这样的东西

 <TextBox Text="{Binding P1}">
    <TextBox.Style>
       <Style TargetType="{x:Type TextBox}">
         <Style.Triggers>
           <Trigger Property="IsFocused" Value="True">
             <Setter Property="{Binding P1Selected}" Value="True"/>
           </Trigger>
         </Style.Triggers>
       </Style>
    </TextBox.Style>
 </TextBox>

其中 P1 和 P1Selected 是 viewModel 属性。 当然,这段代码不起作用。我写它只是为了给出想法..

【问题讨论】:

    标签: wpf mvvm focus


    【解决方案1】:

    对不起,我的回答错了。 请参阅OneWayToSource binding from readonly property in XAML

    【讨论】:

    • 您无法绑定 IsFocused。因为它是只读属性。
    • 你仍然不能这样做。见stackoverflow.com/questions/658170/…
    • "'IsFocused' 属性是只读的,不能从标记中设置。"适用于 OneWay 和 OneWayToSource
    • 感谢@Amit 链接,特别是this answer,我已经解决了这个问题
    猜你喜欢
    • 1970-01-01
    • 2020-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 2011-06-27
    • 1970-01-01
    相关资源
    最近更新 更多