我想第一次与我的C#代码确定什么颜色xaml将使文本在自定义文本框中的绑定。数据触发器和绑定不起作用
我的C#代码:
public class Limits
{
public static bool fruitLimits(string textboxDec, ComboBox boxVariable)
{
if (string.IsNullorWhiteSpace(textboxDec)
{return false;}
else if (boxVariable.SelectedIndex == 1)
{
try
{ int apples = Convert.ToInt32(textboxDex);
if (apples < 4 && apples != 0)
{return false;}
else if(apples > 50)
{return false;}
else
return true;
}
catch (FormatException fEx)
{return false;}
}
else
{
try
{ int oranges = Convert.ToInt32(textboxDec);
if (oranges < 1 && oranges != 0)
{
return false;}
else if (oranges > 100)
{return false;}
else
return true;
}
catch (FormatException fEx2)
{return false;}
}
}
所以现在我想结合这个方法为XAML所以当此方法返回true,框中的文字是黑色的,当它返回false,该文本是红色的。
<local:DigitBox x:Name="FruitNumber">
<local:DigitBox.Style>
<Style TargetType="local:DigitBox">
<Style.Triggers>
<DataTrigger
Binding="{Binding Limits.fruitLimits}" Value="False">
<Setter Property="Foreground" Value="Red"/>
</DataTrigger>
</Style.Triggers>
</Style>
</local:DigitBox.Style>
</local:DigitBox>
所以没有发现错误,但我的自定义文本框不会改变颜色。我尝试直接在我的c#方法中设置颜色更改,并且工作。但我正在努力遵守我一直在阅读的内容,这些内容是保持xaml中的视觉变化。这需要约束力,但我显然缺少/不理解关键的东西
有时是很有帮助的读一本书或有关技术要领* *前尝试使用它的文章。对于WPF数据绑定,我建议阅读MSDN上的[数据绑定概述](https://msdn.microsoft.com/en-us/library/ms752347(v = vs.110).aspx)文章理解。 – Clemens