简体   繁体   中英

Binding static class to contentcontrol wpf

I am trying to make a form for some settings that I want to use in my application. Because I don't know how else to achieve this I'm using a static class to hold the settings for me, they are going to be used on another page.

Here is my View for that page, and below it the static class I'm trying to use.

<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

}

My problem is that the settings I have specified in the class are not currently showing up and I don't know how to properly bind the data itself to the content control (So that's why its just left as {Binding} at the minute). I think I'm missing something and I need to separate out the template and the data but I don't know how/which tags to use.

I also want to save the settings but only on the press of the button. I read that I can achieve this by using Update source triggers on the binded textboxes/combo and then calling the update through the button Command which is currently just a blank ICommand. Will this work outside of the data template or will I need to move the button inside it?

Thanks

If you want oneway binding (textblock) you can use x:Static Member in binding expression

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

And you can't use static class in twoway binding(TextBox), because you need use the Path in binding expression. But you can use Singleton pattern in your static class and then you can use static binding (see here )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM