WPF ComboBox 是格式化 ItemsSource 的更好方法

发布于 2024-08-20 20:42:04 字数 2031 浏览 2 评论 0原文

早上好,伙计们,

我有几个绑定到 TimeSpan 列表的组合框。我正在使用 IValueConverter 和 ItemTemplate 格式化 TimeSpans。我想知道是否有更简单的方法来格式化时间跨度。这就是我目前正在做的事情。

public class TimeSpanConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

        TimeSpan t = TimeSpan.MinValue;
        TimeSpan.TryParse(value.ToString(), out t);
        return "{0:00}:{1:00}".F(t.Hours,t.Minutes);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return TimeSpan.Parse(value.ToString());
    }

    #endregion
}

<Canvas>
    <Canvas.Resources>
      <bc:TimeSpanConverter x:Key="ts" />
      <DataTemplate x:Key="TimeSpanTemplate">
        <TextBlock Text="{Binding ., Converter={StaticResource ts}}" />
      </DataTemplate>

    </Canvas.Resources>
    <TextBlock Canvas.Left="6"
               Canvas.Top="6"
               Height="21"
               Name="textBlock4"
               Text="Begin"
               Width="40" />

    <TextBlock Canvas.Left="81"
               Canvas.Top="6"
               Height="21"
               Name="textBlock5"
               Text="End"
               Width="40" />
    <ComboBox Canvas.Left="7"
              Canvas.Top="25"
              Height="23"
              Name="TimeBeginCombo"
              ItemTemplate="{StaticResource TimeSpanTemplate}"
              SelectedItem="{Binding TimeBegin}"                  
              Width="68" />
    <ComboBox Canvas.Left="81"
              Canvas.Top="25"
              Height="23"
              Name="TimeEndCombo"
              ItemTemplate="{StaticResource TimeSpanTemplate}"
              SelectedItem="{Binding TimeEnd}"
              Width="68" />

  </Canvas>
</GroupBox>

Morning Guys,

I have a few ComboBoxes bound to List of TimeSpan. I am formatting the TimeSpans using IValueConverter and ItemTemplate. I was wondering if there were an easier way to format the TimeSpans. Here's what I'm currently doing.

public class TimeSpanConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value == null)
            return string.Empty;

        TimeSpan t = TimeSpan.MinValue;
        TimeSpan.TryParse(value.ToString(), out t);
        return "{0:00}:{1:00}".F(t.Hours,t.Minutes);
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return TimeSpan.Parse(value.ToString());
    }

    #endregion
}

<Canvas>
    <Canvas.Resources>
      <bc:TimeSpanConverter x:Key="ts" />
      <DataTemplate x:Key="TimeSpanTemplate">
        <TextBlock Text="{Binding ., Converter={StaticResource ts}}" />
      </DataTemplate>

    </Canvas.Resources>
    <TextBlock Canvas.Left="6"
               Canvas.Top="6"
               Height="21"
               Name="textBlock4"
               Text="Begin"
               Width="40" />

    <TextBlock Canvas.Left="81"
               Canvas.Top="6"
               Height="21"
               Name="textBlock5"
               Text="End"
               Width="40" />
    <ComboBox Canvas.Left="7"
              Canvas.Top="25"
              Height="23"
              Name="TimeBeginCombo"
              ItemTemplate="{StaticResource TimeSpanTemplate}"
              SelectedItem="{Binding TimeBegin}"                  
              Width="68" />
    <ComboBox Canvas.Left="81"
              Canvas.Top="25"
              Height="23"
              Name="TimeEndCombo"
              ItemTemplate="{StaticResource TimeSpanTemplate}"
              SelectedItem="{Binding TimeEnd}"
              Width="68" />

  </Canvas>
</GroupBox>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你爱我像她 2024-08-27 20:42:04

您绑定的内容是 TimeSpan 类型吗?如果是这样,那么转换器可以更简单

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (!(value is TimeSpan))
        return string.Empty;

    TimeSpan t = (TimeSpan)value;
    return "{0:00}:{1:00}".F(t.Hours,t.Minutes);
}

而且,一般性评论 - 您确定需要在 Canvas 上布局 UI 并使用绝对坐标吗? :)

Is what you're binding to of type TimeSpan? If so, then the converter can be much simpler

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (!(value is TimeSpan))
        return string.Empty;

    TimeSpan t = (TimeSpan)value;
    return "{0:00}:{1:00}".F(t.Hours,t.Minutes);
}

And also, a general remark - are you sure you need to layout your UI on a Canvas and using absolute coordinates? :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文