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}了)。我想我遗漏了一些东西,我需要将模板和数据分开,但我不知道如何/使用哪些标签。

我也想保存设置,但只能在按下按钮时保存。我读到我可以通过在绑定的文本框/组合上使用更新源触发器然后通过按钮命令调用更新来实现这一点,该按钮命令目前只是一个空白的 ICommand。这将在数​​据模板之外工作还是我需要在其中移动按钮?

谢谢

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

而且你不能在双向绑定(文本框)中使用静态类,因为你需要在绑定表达式中使用路径。但是你可以在你的静态类中使用单例模式,然后你可以使用静态绑定(见这里

于 2013-06-21T16:15:01.410 回答