【发布时间】:2020-10-31 09:37:56
【问题描述】:
- 我有一个
struct类,它记录一个值和一个检查,它只是说明它是好还是坏。
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并将其隐藏在另一个文本块中?
非常感谢。
【问题讨论】:
-
对不起。我真的不能投票,因为我没有足够的积分。在将您的回复标记为答案之前,我希望能从其他人那里得到不同的答案。但是,我想我不会得到更多答案,因为我的问题有点初级。无论如何,给你!谢谢
标签: c# wpf textblock dependencyobject