[英]Better way to trigger color change based on displayed text
struct
class 記錄一個值和一個檢查,它只是說明它是好還是壞。 public enum StressCheck
{
Good,
Bad
}
public struct Stress
{
public Stress(double stressValue, StressCheck check)
{
StressValue = stressValue;
Check = check;
}
public double StressValue { get; set; }
public StressCheck Check { get; set; }
}
TextBlock
,當值為“壞”時,其背景變為紅色。 這就是我目前在我的用戶控件的 XAML 中所做的。<UserControl x:Class="ScienceProgram.UserControls.DataCellCheck"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
x:Name="parent">
<UserControl.Resources>
<Style x:Key="CheckStress" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=TB,Path=Text}" Value="Good">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding ElementName=TB,Path=Text}" Value="Bad">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
</UserControl.Resources>
<Grid Margin="1" DataContext="{Binding ElementName=parent}" Width="100">
<TextBlock Style="{StaticResource CheckStress}" Text="{Binding Path=Value}" />
<TextBlock x:Name="TB" Text="{Binding Path=Check}" Visibility="Collapsed"/>
</Grid>
以及依賴對象“Value”和“Check”的標准代碼隱藏
public partial class DataCellCheck : UserControl
{
public DataCellCheck()
{
InitializeComponent();
}
public static readonly DependencyProperty ValueProp =
DependencyProperty.Register("Value", typeof(string),
typeof(DataCellCheck), new PropertyMetadata(""));
public string Value
{
get { return GetValue(ValueProp) as String; }
set { SetValue(ValueProp, value); }
}
public static readonly DependencyProperty StatusProp =
DependencyProperty.Register("Check", typeof(string),
typeof(DataCellCheck), new PropertyMetadata(""));
public string Check
{
get { return GetValue(StatusProp) as String; }
set { SetValue(StatusProp, value); }
}
}
TextBlock
綁定到折疊的Texblock
來更改Background
顏色,其中我以以下方式綁定來自 viewModel 的StressCheck
值。 public class TestViewModel : BindableBase
{
public Stress MyFirstStress { get; set; }
public TestViewModel()
{
MyFirstStress = new Stress(1245, StressCheck.Fail);
}
public double DisplayStressValue => MyFirstStress.StressValue;
public string DisplayStressCheck => MyFirstStress.Check.ToString();
}
並在 XAML
<UserControl x:Class="ScienceProgram.Views.TestView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:ScienceProgram.Views"
xmlns:uc="clr-namespace:ScienceProgram.UserControls"
mc:Ignorable="d"
d:DesignHeight="1200" d:DesignWidth="811">
<Grid Margin="5">
<Grid.RowDefinitions>
<RowDefinition Height="65"/>
</Grid.RowDefinitions>
<StackPanel>
<uc:DataCellCheck Value="{Binding Path=DisplayStressValue }" Check="{Binding Path=DisplayStressCheck}"/>
</StackPanel>
</Grid>
</UserControl>
StressCheck
並將其隱藏在另一個文本塊中?非常感謝。
有沒有更好的方法來觸發顯示的文本塊中的顏色變化,而不必綁定 StressCheck 並將其隱藏在另一個文本塊中?
我應該希望如此。 你現在這樣做的方式非常笨拙。
如果沒有最小、完整、可驗證的代碼示例,就不可能確認這在您的確切場景中是否有效,但它應該。 您可以綁定到觸發器中的Check
屬性而不是其他TextBlock
:
<Style x:Key="CheckStress" TargetType="TextBlock">
<Style.Triggers>
<DataTrigger Binding="{Binding Check}" Value="Good">
<Setter Property="Background" Value="Green" />
</DataTrigger>
<DataTrigger Binding="{Binding Check}" Value="Bad">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
對於它的價值,我更喜歡為一個值包含一個設置器,並為另一個(或其他,如果有多個)使用觸發器:
<Style x:Key="CheckStress" TargetType="TextBlock">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Check}" Value="Bad">
<Setter Property="Background" Value="Red" />
</DataTrigger>
</Style.Triggers>
</Style>
無論哪種方式,WPF 隱式處理enum
類型,將提供的文本值解析為實際枚舉值並將其用於觸發器的比較。 您不需要額外的TextBlock
或其他機制(例如轉換器)來在您放入 XAML 的文本和實際enum
值之間進行轉換。
如果以上內容不能解決您的問題,請通過提供良好的 MCVE 來改進問題,以便可以理解您的場景的完整上下文。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.