WPF selectitem值绑定

时间:2015-09-21 07:26:30

标签: c# wpf dictionary

我有带有绑定字典的组合框

字典:

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; }

TimeItems

4 个答案:

答案 0 :(得分:1)

您可以使用converter将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}}" />

答案 1 :(得分:0)

如果您只想显示和检索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>

其中local是包含转换器的项目的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

答案 2 :(得分:0)

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

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

答案 3 :(得分:-1)

试试这个:

$cordovaOauth.instagram("CLIENT_ID", ["basic","comments", "relationships"]).then(function(result) {
  console.log(result);
});