我正在使用以下内容使用 TimeZone Info 填充我的组合框:


private void Window_Loaded(object sender, RoutedEventArgs e)
{
    ReadOnlyCollection<TimeZoneInfo> TimeZones = TimeZoneInfo.GetSystemTimeZones();
    this.DataContext = TimeZones;
    cmb_TZ.SelectedIndex = 1;
}


<ComboBox x:Name="cmb_TZ" ItemsSource="{Binding}"   Grid.Row="0" Grid.Column="2" Height="28.5" Margin="10,65.375,30.945,0" VerticalAlignment="Top" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin" SelectionChanged="ComboBox_Selection"/>

我还可以使用以下代码在 文本框 中显示相应的值:
private void ComboBox_Selection(object Sender, SelectionChangedEventArgs e)
{
    var cmbBox = Sender as ComboBox;
    DateTime currTime = DateTime.UtcNow;
    TimeZoneInfo tst = (TimeZoneInfo)cmbBox.SelectedItem;
    txt_Time.Text = TimeZoneInfo.ConvertTime(currTime, TimeZoneInfo.Utc, tst).ToString("HH:mm:ss dd MMM yy");
}

其中 txt_Time 是我的文本框。它的 XAML 代码是:
 <TextBox x:Name="txt_Time" Grid.Row="0" Grid.Column="1" Height="28.5" Margin="26.148,65.375,28.13,0" TextWrapping="Wrap"  VerticalAlignment="Top" d:LayoutOverrides="LeftMargin, RightMargin, TopMargin, BottomMargin"/>



有没有办法使用数据绑定(bind)来实现这一点?
我可以使用上面显示的直接方法来做到这一点。但我想知道这个计算是否可以通过数据绑定(bind)完成?


我是 C#/WPF 的新手,我尝试创建一个简单的类和一个使用 INotifyPropertyChanged 并在 MainWindow 构造函数中引用它的类,但我什至无法填充组合框。

我真的很想了解和使用 C# 的数据绑定(bind)魔法。

最佳答案

在标准的 MVVM 方法中,您将创建一个具有两个属性的 View 模型类,一个用于所有 TimeZoneInfos 的只读集合,另一个用于当前选定的 TimeZone。

public class ViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public ReadOnlyCollection<TimeZoneInfo> TimeZones { get; }
        = TimeZoneInfo.GetSystemTimeZones();

    private TimeZoneInfo selectedTimeZone = TimeZoneInfo.Local;

    public TimeZoneInfo SelectedTimeZone
    {
        get { return selectedTimeZone; }
        set
        {
            selectedTimeZone = value;
            PropertyChanged?.Invoke(this,
                new PropertyChangedEventArgs("SelectedTimeZone"));
        }
    }
}

您可以将窗口的 DataContext 设置为 View 模型的实例,并绑定(bind) ComboBox 和 TextBox 属性,如以下 XAML 所示:
<Window ...>
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
    <Window.Resources>
        <local:TimeZoneConverter x:Key="TimeZoneConverter" />
    </Window.Resources>
    <StackPanel>
        <ComboBox ItemsSource="{Binding TimeZones}"
                  SelectedItem="{Binding SelectedTimeZone}" />
        <TextBox Text="{Binding SelectedTimeZone,
                        Converter={StaticResource TimeZoneConverter}}"/>
    </StackPanel>
</Window>

其中 Binding to the Text 属性使用像这样的转换器:
public class TimeZoneConverter : IValueConverter
{
    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        return value == null ? string.Empty : TimeZoneInfo
            .ConvertTime(DateTime.UtcNow, TimeZoneInfo.Utc, (TimeZoneInfo)value)
            .ToString("HH:mm:ss dd MMM yy");
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

关于c# - 使用数据绑定(bind),根据组合框中选定的时区在文本框中显示时间,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37827704/

10-12 00:26
查看更多