繁体   English   中英

WPF selectitem值绑定

[英]WPF selecteditem value binding

我有绑定字典的组合框

字典:

public Dictionary<string,DateTime> TimeItems { get; set; }

<ComboBox Grid.Column="3"
      HorizontalContentAlignment="Center"
      VerticalContentAlignment="Center"
      ItemsSource="{Binding TimeItems}"
      SelectedIndex="0"
      SelectedItem="{Binding SelectedItem}">

我如何绑定到public DateTime SelectedItem { get; set; } public DateTime SelectedItem { get; set; } public DateTime SelectedItem { get; set; } TimeItems

您可以使用转换器将Dictionary的值绑定到SelectedItem。

public class DictionaryValueConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return value;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return ((KeyValuePair<string, DateTime>)value).Value;
    }
}

XAML

<ComboBox Grid.Column="3" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"
    ItemsSource="{Binding TimeItems}"
    SelectedItem="{Binding SelectedItem, Converter={StaticResource DictionaryValueConverter}}" />

如果仅对显示和检索Dcitionary的值感兴趣,则可以使用以下选项

视图模型

class ExpenseViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(string propName)
    {
        var pc = PropertyChanged;
        if (pc != null)
        {
            pc(this, new PropertyChangedEventArgs(propName));
        }
    }

    private void Populate()
    {
        _mySource.Add("Test 1", DateTime.Now);
        _mySource.Add("Test 2", DateTime.Now.AddDays(1));
        _mySource.Add("Test 3", DateTime.Now.AddDays(2));
        _mySource.Add("Test 4", DateTime.Now.AddDays(3));
        NotifyPropertyChanged("MySource");
    }
    private Dictionary<string, DateTime> _mySource;
    public Dictionary<string, DateTime> MySource
    {
        get { return _mySource; }
    }

    public ExpenseViewModel()
    {
        _mySource = new Dictionary<string, DateTime>();
        Populate();
    }
    public DateTime SelectedDate
    {
        get;
        set;
    }
}

View.xaml

<StackPanel HorizontalAlignment="Left">
    <StackPanel.Resources>
        <local:Converters x:Key="Converter"/>
    </StackPanel.Resources>
    <ComboBox x:Name="cmbBox" Width="200" Margin="10,0,20,0" ItemsSource="{Binding MySource, Converter={StaticResource Converter}}" 
          SelectedItem="{Binding SelectedDate, Mode=TwoWay}" Height="20">

    </ComboBox>
</StackPanel>

其中本地是包含转换器Converter的项目的xmlns

public class Converters : IValueConverter
{
    private static readonly PropertyInfo InheritanceContextProp = typeof(DependencyObject).GetProperty("InheritanceContext", BindingFlags.NonPublic | BindingFlags.Instance);

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var dic = value as Dictionary<string, DateTime>;
        return dic.Values;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

鉴于代码背后,我刚刚定义了datacontext

您可以将SelectedValuePath设置为“ Value”(因为每个项目都是一个KeyValuePair),然后将SelectedValue属性绑定到您的属性:

<ComboBox Grid.Column="3"
  HorizontalContentAlignment="Center"
  VerticalContentAlignment="Center"
  ItemsSource="{Binding TimeItems}"
  SelectedIndex="0"
  SelectedValuePath="Value"
  SelectedValue="{Binding SelectedItem}"/>

尝试这个:

foreach (KeyValuePair<string,DateTime> values in TimeItems){
         comboBox.Items.Add(values.Value);
         }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM