我想第一次與我的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