【问题标题】:Button`s content on change按钮更改的内容
【发布时间】:2016-10-14 12:24:24
【问题描述】:

我有一个 WPF-MVVM 应用程序,并且我正在将按钮的内容与 ViewModel 绑定。 有时会发生新内容包含“()”内的文本。

   <Button Grid.Row="0" Content="{Binding UnfinishedText}"
                            FontSize="22" Grid.Column="7"
                            Height="Auto" Foreground="White"
                            Style="{StaticResource MaterialDesignToolButton}"
                            Command="{Binding ShowUnfinishedProcedures}"
                            CommandParameter="{Binding ElementName=MainWindow}"/>


 public string ToDoText
        {
            get { return _toDoText; }
            set
            {
                _toDoText = value;
                RaisePropertyChanged("ToDoText");
            }
        }

假设我绑定了新内容:“Hello(60)”。是否可以在此示例中仅为括号中的文本更改前景?

我计划使用Button.ContentChanged 事件但它不存在。

谁能帮帮我?

【问题讨论】:

    标签: c# wpf button mvvm


    【解决方案1】:

    对 Alex 建议的补充。

    在您的视图模型中创建两个属性,一个用于文本,一个用于括号内的值。

    这里以消息和计数为例。

    如下所示绑定两个属性以保持简单。

     <Button.Content>
        <TextBlock>
            <Run Text="{Binding Message}"/>(            
            <Run Text="{Binding Count}" Style="{StaticResource SomeStyle}"/>)            
        </TextBlock>
     </Button.Content>
    

    如果需要,您还可以附加一些静态文本。

        <TextBlock>
            <Run Text="StatiContent" Foreground="Blue"/>
            <Run Text="{Binding Message}"/>(            
            <Run Text="{Binding Count}" Style="{StaticResource SomeStyle}"/>)            
        </TextBlock>
    

    【讨论】:

      【解决方案2】:

      在 WPF 中,您可以嵌套 TextBlocks 并对它们应用不同的样式和绑定。

      因此,在您的 ViewModel 中,您可以有两个单独的属性,每个属性都绑定到不同的 TextBlock 并具有自己的样式:

      private string _message = null;
      public string Message
      {
          get
          {
              return _message;
          }
          set
          {
              if (_message == value)
              {
                  return;
              }
      
              _message = value;
              RaisePropertyChanged(() => Message);
          }
      }
      
      private int _count = 0;
      public int Count
      {
          get
          {
              return _count;
          }
          set
          {
              _count = value;
              RaisePropertyChanged(() => Count);
          }
      }
      

      XAML:

      <Button Grid.Row="0"
              FontSize="22" Grid.Column="7"
              Height="Auto" Foreground="White"
              Style="{StaticResource MaterialDesignToolButton}"
              Command="{Binding ShowUnfinishedProcedures}"
              CommandParameter="{Binding ElementName=MainWindow}">
          <Button.Content>
              <TextBlock><TextBlock Text="{Binding Message}" /> (<TextBlock Text="{Binding Count}" Style="{StaticResource CountTextBlockStyle}" />)</TextBlock>
          </Button.Content>
      </Button>
      

      【讨论】:

        猜你喜欢
        • 2013-03-11
        • 1970-01-01
        • 1970-01-01
        • 2010-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多