0

私はプラグイン システムを構築していますが、できるだけ動的にしたいので、そのための設定パネルも必要です。このために、Dictionary から設定ページを作成します。ここで、TKey はパラメーターのラベルを定義し、TValue はパラメーターの型を定義します。ただし、実際の UI を生成する簡単な方法をまだ見つけていません。私は多くのことをしたくありません。それは、事前定義された TextBlock-TextBox ペアを持つ単純な StackPanel であり、これらのそれぞれが Dictionary エントリの 1 つを表します。

これを簡単に行うにはどうすればよいでしょうか。

4

1 に答える 1

0

Aが返す は不変であるため (したがって、双方向バインディングでは使用できません) Dictionary、バインディングには最適ではありません。KeyValuePair

それでもデータをディクショナリに保持したい場合は、キーを含むクラスでラップすることができます (何かを に表示するためTextBlock)。それを行う1つの方法:

// some classes to represent our settings
public abstract class Parameter
{
    public string Key { get; private set; }
    public Parameter( string key ) { this.Key = key; }
}

public class StringParameter : Parameter
{
    public string Value { get; set; }
    public StringParameter( string key, string value ) : base( key )
    {
        this.Value = value;
    }
}

いくつかのテストデータ:

public Dictionary<string, Parameter> m_Settings = new Dictionary<string, Parameter>();
// NOTE: we're returning the dictionary values here only
public IEnumerable<Parameter> Settings { get { return m_Settings.Values; } }

...

var p1 = new StringParameter( "Parameter 1", "Value 1" );
var p2 = new StringParameter( "Parameter 2", "Value 2" );
var p3 = new StringParameter( "Parameter 3", "Value 3" );

m_Settings.Add( p1.Key, p1 );
m_Settings.Add( p2.Key, p2 );
m_Settings.Add( p3.Key, p3 );

非常にシンプルな UI:

<Window ...
        xmlns:self="clr-namespace:WpfApplication8"
        DataContext="{Binding RelativeSource={RelativeSource Self}}" >
    <ItemsControl ItemsSource="{Binding Settings}">
        <ItemsControl.Resources>
            <DataTemplate DataType="{x:Type self:StringParameter}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Key}"/>
                    <TextBox Text="{Binding Value, Mode=TwoWay}"/>
                </StackPanel>
            </DataTemplate>
        </ItemsControl.Resources>
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <StackPanel Orientation="Vertical"/>
            </ItemsPanelTemplate>            
        </ItemsControl.ItemsPanel>
    </ItemsControl>
</Window>

汎用ベースを持つことで、Parameterさまざまな種類の設定をすべて独自のDataTemplate. これにはすべて、検証と必要なものがすべて欠けていることに注意してください。

于 2015-05-03T10:09:24.283 に答える