c# - 使用数据绑定(bind),根据 ComboBox 中选定的时区在文本框中显示时间

标签 c# wpf xaml data-binding

我使用下面的时区信息填充我的组合框:

MainWindow.xaml.cs

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

The below is from the XAML:

<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"/>

My question is :

有没有办法使用数据绑定(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 属性使用这样的 Converter:

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),根据 ComboBox 中选定的时区在文本框中显示时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37827704/

相关文章:

c# - EF Code First 如何为派生类创建单独的表?

c# - 如何为位于 Http Url 中的文件生成 MD5 哈希?

wpf - 当绑定(bind)值更改时突出显示 WPF DataGrid 中的单元格

Silverlight Treeview 内联 HierarchicalDataTemplate 绑定(bind)问题

c# - C++ 和 C# 调用 CryptEncrypt 和 CryptDecrypt

c# - XElement Nodes() 与 Elements() 有什么区别?

c# - 将数据库中的所有列自动添加到 INSERT INTO 查询

c# - 当用户按下输入按钮 wpf 时,如何在键盘上触发按键事件

c# - 在属性节点中绑定(bind)

xaml - 如何在ListView中绑定(bind)Label的FontSize