WPF TextBlock绑定到后面的代码

时间:2018-06-14 11:40:03

标签: c# wpf binding

我有这样的CS代码:

public static string days(DateTime fromDate)
    {
       switch (fromDate.DayOfWeek)
        {
            //Weekdays
            case DayOfWeek.Monday:
                return "Wednesday";
            case DayOfWeek.Tuesday:
                return "Thursday and Friday";
            case DayOfWeek.Wednesday:
                return "Saturday";
            case DayOfWeek.Thursday:
                return "Sunday and Monday";
            case DayOfWeek.Friday:
                return "Tuesday";
            //Weekends
            default:
                return "-";
        }
    }

现在我想将它绑定到文本块:

<TextBlock Name="days" HorizontalAlignment="Left" Margin="625,230,0,0" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="159" Height="21" TextAlignment="Center"/>

你能帮我怎么做吗?

2 个答案:

答案 0 :(得分:0)

直接设置Text的{​​{1}}属性:

TextBlock

或者从视图模型的公共属性中调用days方法并绑定到此方法。

答案 1 :(得分:0)

你创造了一个属性DayNow
public string DayNow { get; set; }
之后,您为其设置值DayNow = days(DateTime.Now)
在视图中绑定您为Text="{Binding DayNow}"添加此属性TextBlock