我很新来WPF,仍然试图围绕XAML绑定我的头。WPF绑定My.Settings集合到Combobox项目
我想用my.settings中的字符串集合的值填充组合框。我能做到这一点在这样的代码:
Me.ComboBox1.ItemsSource = My.Settings.MyCollectionOfStrings
...和它的作品。
我该如何在我的XAML中执行此操作?可能吗?
感谢
我很新来WPF,仍然试图围绕XAML绑定我的头。WPF绑定My.Settings集合到Combobox项目
我想用my.settings中的字符串集合的值填充组合框。我能做到这一点在这样的代码:
Me.ComboBox1.ItemsSource = My.Settings.MyCollectionOfStrings
...和它的作品。
我该如何在我的XAML中执行此操作?可能吗?
感谢
是,你可以(也应该在大多数情况下)在XAML中声明绑定,因为这是在WPF中最强大的功能之一。
在你的情况下,将ComboBox绑定到您的自定义设置,您可以使用下面的XAML之一:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:WpfApplication1.Properties"
Title="Window1">
<StackPanel>
<ComboBox
ItemsSource="{Binding Source={x:Static p:Settings.Default}, Path=MyCollectionOfStrings}" />
</StackPanel>
</Window>
注意以下几个方面:
这是可能的。在C#中,我做这样的(一个简单的布尔):
IsExpanded="{Binding Source={StaticResource Settings}, Mode=TwoWay, Path=Default.ASettingValue}"
我在App.xaml中的Application.Resources定义静态资源“设置”正是如此:
<!-- other namespaces removed for clarity -->
<Application xmlns:settings="clr-namespace:DefaultNamespace.Properties" >
<Application.Resources>
<ResourceDictionary>
<settings:Settings x:Key="Settings" />
<!--stuff removed-->
</ResourceDictionary>
</Application.Resources>
</Application>
你的路径可能不一样;在C#中,您通过
访问应用程序设置DefaultNamespace.Properties.Settings.Default.ASettingValue
知道了!
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:p="clr-namespace:WpfApplication1"
Title="Window1" Height="90" Width="462" Name="Window1">
<Grid>
<ComboBox ItemsSource="{Binding Source={x:Static p:Settings.Default}, Path=MyCollectionOfStrings}" />
</Grid>
</Window>
谢谢大家帮助我达到一个伟大的“啊哈!”时刻:-) ......希望在WPF上花更多时间之后,我会理解为什么这会起作用。
我有一个更简单的解决方案来做到这一点,使用自定义标记扩展。在你的情况下,它可以像这样使用:
<Window x:Class="Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1" Height="90" Width="462" Name="Window1">
<Grid>
<ComboBox ItemsSource="{my:SettingBinding MyCollectionOfStrings}" />
</Grid>
</Window>
你可以找到的C#代码在我的博客此标记扩展的位置: http://www.thomaslevesque.com/2008/11/18/wpf-binding-to-application-settings-using-a-markup-extension/
您也可以在列表存储在设置分隔字符串然后使用转换器。
<ComboBox ItemsSource="{Binding Default.ImportHistory,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay,Converter={StaticResource StringToListConverter},ConverterParameter=|}" IsEditable="True">
/// <summary>
/// Converts a delimited set of strings to a list and back again. The parameter defines the delimiter
/// </summary>
public class StringToListConverter : IValueConverter {
/// <summary>
/// Takes a string, returns a list seperated by {parameter}
/// </summary>
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
string serializedList = (value ?? string.Empty).ToString(),
splitter = (parameter ?? string.Empty).ToString();
if(serializedList.Trim().Length == 0) {
return value;
}
return serializedList.Split(new[] { splitter }, StringSplitOptions.RemoveEmptyEntries);
}
/// <summary>
/// Takes a list, returns a string seperated by {parameter}
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
var items = value as IEnumerable;
var splitter = (parameter ?? string.Empty).ToString();
if(value == null || items == null) {
return value;
}
StringBuilder buffer = new StringBuilder();
foreach(var itm in items) {
buffer.Append(itm.ToString()).Append(splitter);
}
return buffer.ToString(0, splitter.Length > 0 ? buffer.Length - splitter.Length : buffer.Length);
}
}
然后单击浏览按钮时,您可以添加到列表:
var items = Settings.Default.ImportHistory.Split('|');
if(!items.Contains(dlgOpen.FileNames[0])) {
Settings.Default.ImportHistory += ("|" + dlgOpen.FileNames[0]);
}
cboFilename.SelectedValue = dlgOpen.FileNames[0];
Settings.Default.Save();
如何标记一个答案为“回答”? :) – 2010-08-10 16:09:29