0

アプリケーションで使用したい設定のフォームを作成しようとしています。これを達成する他の方法がわからないため、静的クラスを使用して設定を保持しています。それらは別のページで使用されます。

これがそのページのビューで、その下に使用しようとしている静的クラスがあります。

<UserControl x:Class="Board.PortSettingView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
         xmlns:local="clr-namespace:Board"
         xmlns:sys="clr-namespace:System;assembly=mscorlib"
         xmlns:io="clr-namespace:System.IO.Ports;assembly=System"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="400">

<UserControl.Resources>
    <!-- Enumerations to populate the comboboxes -->
    <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="StopBits">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="io:StopBits" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="Parity">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="io:Parity" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>
    <ObjectDataProvider ObjectType="{x:Type io:SerialPort}" MethodName="GetPortNames" x:Key="portNames"/>

    <!-- Data Template for Settings -->
    <DataTemplate x:Key="Data" DataType="{x:Type local:Data}">
        <Border HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" BorderBrush="Black" BorderThickness="1" Padding="5">
            <Grid Width="150" HorizontalAlignment="Left" VerticalAlignment="Top">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                    <RowDefinition />
                </Grid.RowDefinitions>

                <TextBlock Grid.Column="0" Grid.Row="0"  Text="Port" VerticalAlignment="Center" Margin="5" />
                <ComboBox SelectedValue="{Binding Com}" Grid.Row="0" Grid.Column="1" Margin="5" ItemsSource="{Binding Source={StaticResource portNames}}" />

                <TextBlock Grid.Column="0" Grid.Row="1"  Text="Baud Rate" VerticalAlignment="Center" Margin="5" />
                <TextBox Grid.Row="1" Grid.Column="1"  Text="{Binding Baud}" Margin="5" />

                <TextBlock Grid.Column="0" Grid.Row="2"  Text="Data Bits" VerticalAlignment="Center" Margin="5" />
                <TextBox Grid.Row="2" Grid.Column="1"  Text="{Binding DB}" Margin="5" />

                <TextBlock Grid.Column="0" Grid.Row="3"  Text="Stop Bits" VerticalAlignment="Center" Margin="5" />
                <ComboBox SelectedValue="{Binding SB}" Grid.Row="3" Grid.Column="1" Margin="5" ItemsSource="{Binding Source={StaticResource StopBits}}" />

                <TextBlock Grid.Column="0" Grid.Row="4"  Text="Parity" VerticalAlignment="Center" Margin="5" />
                <ComboBox SelectedValue="{Binding Par}" Grid.Row="4" Grid.Column="1" Margin="5" ItemsSource="{Binding  Source={StaticResource Parity}}" />

            </Grid>
        </Border>
    </DataTemplate>
</UserControl.Resources>

<Grid>
    <StackPanel>
    <TextBlock Text="Settings" 
               FontWeight="Bold" FontSize="18"
               HorizontalAlignment="Left" VerticalAlignment="Top"
               Margin="10,2"/>
    <ContentControl Margin="10,2" Content="{Binding}" ContentTemplate="{StaticResource Data}" />
    <Button Content="Save"  Command="{Binding Path=SaveSettingsCommand}" HorizontalAlignment="Left" Height="30" Margin="10,4" VerticalAlignment="Top" Width="75" />
    </StackPanel>
</Grid>

static class Data
{
    #region Fields

    // Regular Port settings
    private static string _com = "COM1";
    private static int _baud = 9600;
    private static int _dB = 8;
    private static StopBits _sB = StopBits.One;
    private static Parity _par = Parity.Even;

    // Advanced
    private static int _tO = 500;
    private static int _rBT = 50;

    #endregion // Fields

    #region Properties

    //Settings
    public static string Com
    {
        get { return _com; }
        set { _com = value; }
    }

    public static int Baud
    {
        get { return _baud; }
        set { _baud = value; }
    }

    public static int DB
    {
        get { return _dB; }
        set { _dB = value; }
    }

    public static StopBits SB
    {
        get { return _sB; }
        set { _sB = value; }
    }

    public static Parity Par
    {
        get { return _par; }
        set { _par = value; }
    }

    public static int TO
    {
        get { return _tO; }
        set { _tO = value; }
    }

    public static int RBT
    {
        get { return _rBT; }
        set { _rBT = value; }
    }

    #endregion

}

私の問題は、クラスで指定した設定が現在表示されておらず、データ自体をコンテンツ コントロールに適切にバインドする方法がわからないことです (そのため、そのままにしておきます{Binding})。何かが足りないので、テンプレートとデータを分離する必要がありますが、どのタグをどのように使用すればよいかわかりません。

また、設定を保存したいのですが、ボタンを押すだけです。バインドされたテキストボックス/コンボで Update source トリガーを使用し、現在は空白の ICommand であるボタン Command を介して更新を呼び出すことで、これを実現できることを読みました。これはデータ テンプレートの外側で機能しますか、それともボタンをデータ テンプレート内に移動する必要がありますか?

ありがとう

4

1 に答える 1

1

一方向バインディング(テキストブロック)が必要な場合は、バインディング式で x:Static Member を使用できます

 <TextBlock Text="{Binding Source={x:Static Member=YourNameSpace:Data.Com}}" Grid.Row="0" Grid.Column="1" Margin="5"  />

また、バインディング式でパスを使用する必要があるため、双方向バインディング(TextBox)で静的クラスを使用することはできません。ただし、静的クラスでシングルトン パターンを使用してから、静的バインディングを使用できます (こちらを参照) 。

于 2013-06-21T16:15:01.410 に答える