8

私は WPF に非常に慣れていませんが、まだ XAML でのバインドに頭を悩ませようとしています。

my.settings の文字列コレクションの値をコンボボックスに入力したいと思います。次のようなコードで実行できます。

Me.ComboBox1.ItemsSource = My.Settings.MyCollectionOfStrings

...そしてそれは機能します。

XAML でこれを行うにはどうすればよいですか? 出来ますか?

ありがとう

4

5 に答える 5

19

はい。WPF で最も強力な機能の 1 つであるため、XAML でバインドを宣言できます (ほとんどの場合、宣言する必要があります)。

あなたの場合、ComboBox をカスタム設定の 1 つにバインドするには、次の 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>

次の点に注意してください。

  • XAML で参照するために、「Settings」クラスが存在する .NET 名前空間を指すプレフィックス「p」を使用して XML 名前空間を宣言しました。
  • XAML でバインディングを宣言するために、マークアップ拡張機能「{Binding}」を使用しました
  • XAML で静的 (VB では「共有」) クラス メンバーを参照することを示すために、マークアップ拡張機能「静的」を使用しました。
于 2008-10-15T14:11:49.290 に答える
3

カスタムマークアップ拡張機能を使用して、それを行うためのより簡単なソリューションがあります。あなたの場合、それはこのように使用することができます:

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

于 2009-04-30T16:23:32.067 に答える
1

可能です。C# では、次のようにします (単純な bool の場合)。

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
于 2008-10-15T14:12:04.797 に答える
1

とった!

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

私が素晴らしい「Aha!」にたどり着くのを手伝ってくれてありがとう。瞬間:-) ...うまくいけば、WPFでもう少し時間を費やした後、これが機能する理由を理解できるでしょう。

于 2008-10-15T16:16:27.230 に答える
0

リストを区切り文字列として設定に保存してから、コンバーターを使用することもできます。

<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();
于 2009-11-06T18:01:50.627 に答える