TextBlock不可变,TextBox(内容)不可见。为什么/什么是首选方法?

时间:2012-02-10 17:54:49

标签: c# wpf wpf-controls

我使用以下代码显示当前日期/时间(.exe实例化时间,即)在第一个日期选择器和TextBlock中减去一天,然后在下一个日期选择器和TextBlock中显示当前日期/时间:< / p>

XAML:

<StackPanel Orientation="Horizontal" >
    <Label>Between</Label>
    <DatePicker Name="dtpFrom" ></DatePicker>
    <TextBlock Name="timeFrom" MinWidth="60" Text="{Binding Date, StringFormat={}{hh:mm: tt}}" />
    <Label>and</Label>
    <DatePicker Name="dtpTo"></DatePicker>
    <TextBlock Name="timeTo"
               MinWidth="60"
               Text="{Binding Date, StringFormat={}{hh:mm: tt}}" />
</StackPanel>

C#:

int dtHour = DateTime.Now.Hour;
int dtMinute = DateTime.Now.Minute;
int dtSecond = DateTime.Now.Second;

dtpFrom.SelectedDate = DateTime.Now.AddDays(-1);
timeFrom.Text = String.Format("{0}:{1}:{2}", dtHour, dtMinute, dtSecond);
timeTo.Text = String.Format("{0}:{1}:{2}", dtHour, dtMinute, dtSecond);
dtpTo.SelectedDate = DateTime.Now;

在TextBlocks中显示时间正常;但它不可编辑。所以我将TextBlocks更改为TextBoxes。现在时间值根本不显示......

~~~~~~~~~~~~~~~~~~~~~~ 更新:

我使用此代码修复了它(尽管有点笨拙):

    int dtHour = DateTime.Now.Hour;
    int dtMinute = DateTime.Now.Minute;
    int dtSecond = DateTime.Now.Second;
    string strHour = string.Empty;
    string strMinute = string.Empty;
    string strSecond = string.Empty;

    if (dtHour < 10)
    {
        strHour = string.Format("0{0}", dtHour.ToString());
    }
    else
    {
        strHour = dtHour.ToString();
    }

    if (dtMinute < 10)
    {
        strMinute = string.Format("0{0}", dtMinute.ToString());
    }
    else
    {
        strMinute = dtMinute.ToString();
    }

    if (dtSecond < 10)
    {
        strSecond = string.Format("0{0}", dtSecond.ToString());
    }
    else
    {
        strSecond = dtSecond.ToString();
    }

    dtpFrom.SelectedDate = DateTime.Now.AddDays(-1);
    timeFrom.Text = String.Format("{0}:{1}:{2}", strHour, strMinute, strSecond);
    timeTo.Text = timeFrom.Text;  //String.Format("{0}:{1}:{2}", strHour, strMinute, strSecond);
    dtpTo.SelectedDate = DateTime.Now;

...而且这个xaml(必须删除“fancy-pants”格式化):

<StackPanel Orientation="Horizontal" >
    <Label>Between</Label>
    <DatePicker Name="dtpFrom" ></DatePicker>
    <TextBox Name="timeFrom" MinWidth="60" />
    <Label>and</Label>
    <DatePicker Name="dtpTo"></DatePicker>
    <TextBox Name="timeTo"
               MinWidth="60" />
</StackPanel>

0 个答案:

没有答案