如何使用MVVM架构在WPF中动态更改按钮标题? [英] How to change the button caption dynamically in WPF using MVVM Architecture?

查看:94
本文介绍了如何使用MVVM架构在WPF中动态更改按钮标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在具有名为XXX.UI.Wpf的启动项目的表示层中,这是我的启动项目,其中仅包含app.xaml和Mainwindow.xaml表单。并且在此haing中有另一个名为XXX.UI.Controls.WPF的项目,所有控件都被写为用户控件。我们正在使用一个功能区控件,如查找,清除,工作历史记录。在单击工作历史记录切换按钮时显示某些内容,同时显示此内容时,工作历史记录标题应更改为返回,但现在它仍保留为工作历史记录。还有一个叫做视图模型的另一层我不知道该层的使用。我是MVVM架构的新手。我也无法看到点击事件。我的用户代码是这样的

Hi,
In the presentation layer having the startup project called XXX.UI.Wpf this is my startup project which has app.xaml and Mainwindow.xaml form alone. And Having Another project called XXX.UI.Controls.WPF in this haing all the controls was written as User control. we are using one Ribbon control like Find, Clear, Work History. While clicking the work history Toggle Button im showing some content while showing this content the Working History caption should has to change as ''Back'' but now it is remain as Work History. Also having a another layer called view model i dont know the use of that layer. I am New to MVVM achitecture. also im unable to see the click event. My User code is like this

<igRibbon:ToggleButtonTool

Id="workHistory"

Caption="Work History"

IsChecked="{Binding IsDetailsSelected}"

HorizontalAlignment="Left"

igRibbon:RibbonGroup.MaximumSize="ImageAndTextLarge"

Command="{Binding WorkOrderHistoryViewModel.LoadWorkOrderHistoryCommand}"

LargeImage="/Gsk.Emt.UI.Controls.Wpf;component/Image/Details.png">

</igRibbon:ToggleButtonTool>





如何达到这个要求?需要帮助。



谢谢

Kannabiran B



How to achieve this requirement? Help required.

Thanks
Kannabiran B

推荐答案

ThePhantomUpvoter是对的(但不完整)。

您需要将 Caption 绑定到封闭的 DataContext (应该实现 INotifyPropertyChanged )。

然后你改变属性值,并确保 PropertyChanged 使用相应的propertyname值引发事件。



但是,如果<的值code> Caption
取决于控件的 IsChecked 值,那么你可以使用 Style 触发器。有点像喜欢这个(我没有你的控制,所以没有测试过这个):

ThePhantomUpvoter is right (but incomplete).
You need to bind the Caption to a property on the enclosing DataContext (which should implement INotifyPropertyChanged).
Then you change the property value, and make sure that the PropertyChanged event is "raised" with the appropriate "propertyname" value.

However, if the value of the Caption depends on the IsChecked value of the control, then you can use the Style with Triggers. Something like this (I don''t have your control, so didn''t test this):
...
    <igRibbon:ToggleButtonTool.Style>
      <Style TargetType="{x:Type igRibbon:ToggleButtonTool}"

        BasedOn="{StaticResource {x:Type igRibbon:ToggleButtonTool}}">
        <Setter Property="Caption"

            Value="Work History" />
        <Style.Triggers>
          <Trigger Property="IsChecked" 

                Value="False">
            <Setter Property="Caption"

                Value="Back" />
          </Trigger>
        </Style.Triggers>
      </Style>
    </igRibbon:ToggleButtonTool.Style>
</igRibbon:ToggleButtonTool>


Hi Matt,

您的解决方案似乎很简单,我喜欢你的方法。我试过这样的代码



Hi Matt,
Your solution seems to be very simple and i like your approach. i tried your code like this

 <igribbon:togglebuttontool xmlns:igribbon="#unknown">
                Id="workHistory"
               Caption="Work History"
                IsChecked="{Binding IsDetailsSelected}"
                HorizontalAlignment="Left"
                igRibbon:RibbonGroup.MaximumSize="ImageAndTextLarge"
                Command="{Binding WorkOrderHistoryViewModel.LoadWorkOrderHistoryCommand}"
                LargeImage="/XXX.Emt.UI.Controls.Wpf;component/Image/Details.png">
 <igribbon:togglebuttontool.style>
      <style targettype="{x:Type igRibbon:ToggleButtonTool}">
        BasedOn="{StaticResource {x:Type igRibbon:ToggleButtonTool}}">
        <setter property="Caption">
            Value="Work History" />
        <style.triggers>
          <trigger property="IsChecked">
                Value="False">
            <setter property="Caption">
                Value="Back" />
          </setter></trigger>
        </style.triggers>
      </setter></style>
    </igribbon:togglebuttontool.style>
</igribbon:togglebuttontool>







但它无法正常工作,请您检查出错的地方?




But its not working, can you please check where its goes wrong?


这篇关于如何使用MVVM架构在WPF中动态更改按钮标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆