簡體   English   中英

在WPF中更改Textblock背景,顏色更改后會自行重置

[英]Changing Textblock background in WPF resetting itself after color change

我在WPF用戶控件中有一個文本塊,定義為:

<TextBlock Grid.Column="0"
           Text="{Binding RecognitionResults}"
           Background="{Binding ResultBackground}" />

顯示TextBlockUserControl從另一個UserControlString形式呈現為:

<ItemsControl ItemsSource="{Binding Strings}" >
      <ItemsControl.ItemContainerStyle>
           <Style>
                <Setter Property="Control.Margin"
                        Value="{Binding Margin}"/>
           </Style>
      </ItemsControl.ItemContainerStyle>
 </ItemsControl>  

本質上, ItemsControl呈現了一個“字符串”列表,每個字符串本身由其自己的UserControl表示。

現在,當我點擊TextBlock的顯示時,手勢將執行以下操作以在ViewModel中將背景顏色從黃色更改為綠色:

public void Refill()
{
    ResultBackground = Brushes.Green;
}

ResultBackground顏色在ViewModel中定義為:

 private SolidColorBrush resultbackground =
     (SolidColorBrush)new BrushConverter().ConvertFromString("#FFEFF100");

 public SolidColorBrush ResultBackground
 {
      get
      {
           Console.WriteLine("Now getting resultbackground of "
               + resultbackground);
           return resultbackground;
      }
      set
      {
          if (resultbackground != value)
          {
              resultbackground = value;
              OnPropertyChanged("ResultBackground");
          }
      }
 }

因此,當我實際點擊TextBlock ,該手勢將成功將其從黃色變為綠色。 到現在為止還挺好。

但是,當我從命令(即菜單命令)執行Refill()方法時, TextBlock首先變成綠色(應有的樣子),然后重新顯示為初始黃色。 觀察上面的Console.WriteLine的輸出,確認首先調用get來使Refill()的框變為綠色,但是隨后(沒有堆棧跟蹤),再次調用get(不調用該集)以檢索黃色!

我完全不知道為什么會發生這種情況或如何解決它。 在代碼中ResultBackground甚至引用ResultBackground地方就是上述代碼。

非常感謝您對此工作的任何幫助。

編輯:我不知道它是否相關,但是執行refill()的命令正在從菜單中作為Action執行,如下所示:

 <UserControl x:Class="Nova5.UI.Views.Ink.InkView"
        ............................

<UserControl.Resources>
    <Style x:Key="MenuItemStyle" TargetType="{x:Type MenuItem}">
         <Setter Property="Command" Value="{Binding OnSelected}" />
    </Style>
</UserControl.Resources>


<Grid>
    <Grid.Resources>
        <HierarchicalDataTemplate DataType="{x:Type m:MyMenuItem}" ItemsSource="{Binding Path=SubItems}">
            <ContentPresenter
                Content="{Binding Path=DisplayText}"
                RecognizesAccessKey="True" />
        </HierarchicalDataTemplate>
    </Grid.Resources>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="25" />
    </Grid.RowDefinitions>

    <Menu Grid.Row="3" Height="28" >
        <MenuItem Header="Options" ItemsSource="{Binding OptionSubItems}" DisplayMemberPath="{Binding DisplayText}" >
            <MenuItem.ItemContainerStyle>
                <Style>
                    <Setter Property="MenuItem.Command"  Value="{Binding OnSelected}"/>
                </Style>
            </MenuItem.ItemContainerStyle>
        </MenuItem>
    </Menu>

</Grid>

其中,在視圖模型構造函數中將OptionSubItems構建為:

  OptionSubItems = new ObservableCollection<MyMenuItem>();

MyMenuItem是:

   public class MyMenuItem : MenuItemBase
{
    private Action Command;

    public MyMenuItem(String DisplayText, Action command)
    {
        this.DisplayText = DisplayText;

        this.Command = command;
    }
    public override void OnItemSelected()
    {
        this.Command();
    }
}

ViewModel構造函數動態生成命令列表,如下所示:

    OptionSubItems.Add(new MyMenuItem("Refill", delegate()
        {
            CurrentViewModelDetails.ExecuteMenuCommand("Refill");
        }));

ExecuteMenuCommand可以:

     if (commandname == "RefillAllCurrentPrescriptions")
        {

            for (int k = 0; k < Strings.Count; k++)
            {
                Strings[k].refill();
            }
        }

希望這可以幫助。 (我想知道問題是否出在上面的xaml中,並且具有從不同樣式到OnSelected的兩個綁定嗎?)

好吧,冒着完全尷尬的風險,這是引起閃爍問題的原始代碼:

 OptionSubItems.Add(new MyMenuItem("Refill all current prescriptions", delegate()
        {
            CurrentViewModelDetails.ExecuteMenuCommand("RefillAllCurrentPrescriptions");
             CurrentViewModelDetails.RefreshDisplay(RxViews.PrescriptionList);
            DialogTitle = "Current Prescriptions";
        }));

RefreshDisplay()方法從數據庫中重建顯示,完全丟棄的影響refill()可呈現解釋了為什么放置在斷點get{}沒有顯示任何堆棧跟蹤。 創建新對象時,XAML會調用get{} 沒有涉及set{} 因此,上面的代碼實際上運行良好(在我刪除了有問題的行以使SO更簡潔之后)。

為了在應有的信譽下給予榮譽,是瑞秋的建議導致對代表們進行重新研究,但感謝所有人的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM