UWP: связывание нескольких копий UserControl в одном представлении - PullRequest
0 голосов
/ 13 мая 2019

Я создал UserControl с четырьмя текстовыми полями. UserControl будет использоваться для отображения данных из трех разных источников. Пока что я не могу заставить работать сантехнику обратно в интерфейс.

Вот базовая настройка:

MyUserControl.xaml

  • Привязки к каждому текстовому полю

MyUserControl.xaml.cs

  • Свойства зависимостей для текстовых полей
  • Свойство Wrappers {get; set;} для текстовых полей

MyViewModel.cs

  • Свойства для каждого текстового поля

MyView.xaml

  • UserControl1 {привязки к свойствам UserControl1 в MyViewModel}
  • UserControl2 {привязки к свойствам UserControl2 в MyViewModel}
  • UserControl3 {привязки к свойствам UserControl3 в MyViewModel}

Это отлично работало для одного пользовательского элемента управления, но когда у меня их три, я получаю ошибки привязки, и текстовые поля не обновляются. Я удалил привязки из MyUserControl, который исправил ошибку, но я не вижу данных в текстовых полях (я убедился в отладчике, что данные попадают в MyView). Возможно ли то, что я делаю, в UWP, и если да, то как мне связать три копии одного и того же UserControl? Я прочитал документы Microsoft по привязке, но я не сталкивался с несколькими привязками к одному и тому же UserControl.

   public partial class PositionView : UserControl
   {

      // To be able to bind to PositionView content from another UI, PositionView's content is  
      // registered as DependencyProperties.
      #region DependencyPropertyIdentifiers
      public static readonly DependencyProperty LatitudeProperty =
         DependencyProperty.Register("Latitude", typeof(float), typeof(PositionView), null);

      public static readonly DependencyProperty LongitudeProperty =
         DependencyProperty.Register("Longitude", typeof(float), typeof(PositionView), null);

      public static readonly DependencyProperty ElevationProperty =
         DependencyProperty.Register("Elevation", typeof(float), typeof(PositionView), null);

      public static readonly DependencyProperty SatelliteCountProperty =
         DependencyProperty.Register("SatelliteCount", typeof(int), typeof(PositionView), null);

      public static readonly DependencyProperty MGRSProperty =
         DependencyProperty.Register("MGRS", typeof(string), typeof(PositionView), null);
      #endregion


      // Specific Getters/Setters for the DependencyProperties using GetValue/SetValue
      #region PropertyWrappers
      public float Latitude
      {
         get { return (float)GetValue(LatitudeProperty); }
         set { SetValue(LatitudeProperty, value); }
      }

      public float Longitude
      {
         get { return (float)GetValue(LongitudeProperty); }
         set { SetValue(LongitudeProperty, value); }
      }

      public float Elevation
      {
         get { return (float)GetValue(ElevationProperty); }
         set { SetValue(ElevationProperty, value); }
      }

      public string MGRS
      {
         get { return (string)GetValue(MGRSProperty); }
         set { SetValue(MGRSProperty, value); }
      }

      public float SatelliteCount
      {
         get { return (int)GetValue(SatelliteCountProperty); }
         set { SetValue(SatelliteCountProperty, value); }
      }
      #endregion


<UserControl
    x:Class="HHPM_NEXT.Views.PositionView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HHPM_NEXT.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="200"
    d:DesignWidth="460"
    FontSize="26">

    <UserControl.Resources>
        <Style TargetType="TextBox">
            <Setter Property="Margin" Value="3"/>
        </Style>
    </UserControl.Resources>
    <Grid HorizontalAlignment="Stretch">

        <!--<Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width=".5*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width=".5*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
      </Grid.RowDefinitions>-->
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>


        <!--<TextBlock Grid.Row="0" Grid.Column="0" Text="Latitude" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource MessageText}"/>-->
        <TextBox Header="Latitutde" x:Name="LatitudeTextBox" Grid.Row="0" Grid.Column="0"  HorizontalAlignment="Stretch" />

        <!--<TextBlock Grid.Row="1" Grid.Column="0" Text="Longitude" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource MessageText}"/>-->
        <TextBox Header="Longitude" x:Name="LongitudeTextBox" Grid.Row="0" Grid.Column="1"  HorizontalAlignment="Stretch" />

        <!--<TextBlock Grid.Row="2" Grid.Column="0" Text="Elevation" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource MessageText}"/>-->
        <TextBox Header="Elevation" x:Name="ElevationTextBox" Grid.Row="0" Grid.Column="2"  HorizontalAlignment="Stretch"  />

        <!--<TextBlock Grid.Row="0" Grid.Column="3" Text="MGRS" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource MessageText}"/>-->
        <TextBox Header="MGRS" x:Name="MGRSTextBox" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" IsReadOnly="True" HorizontalAlignment="Stretch" />

        <!--<TextBlock Grid.Row="1" Grid.Column="3" Text="Satellites" HorizontalAlignment="Left" VerticalAlignment="Center" Style="{StaticResource MessageText}"/>-->
        <TextBox Header="Satellites" x:Name="SatellitesTextBox" Grid.Row="1" Grid.Column="2" IsReadOnly="True" HorizontalAlignment="Stretch" />

   </Grid>


<Page
    x:Class="HHPM_NEXT.Views.EmplacementView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:HHPM_NEXT"
    xmlns:views="using:HHPM_NEXT.Views"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">


   <ScrollViewer>
      <UserControl>
         <Grid x:Name="EmplacementViewGrid">
               <VisualStateManager.VisualStateGroups>
                  <VisualStateGroup>
                     <VisualState x:Name="VerticalState">
                        <VisualState.StateTriggers>
                           <AdaptiveTrigger MinWindowWidth="0"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                           <!--styles-->
                           <Setter Target="EmplacementViewGrid.Style" Value="{StaticResource VerticalStateGrid}"/>
                           <Setter Target="SavePositionButton.Style" Value="{StaticResource LargeButton}" />
                           <Setter Target="SendPositionTableButton.Style" Value="{StaticResource LargeButton}" />
                           <Setter Target="EditDevicePositionButton.Style" Value="{StaticResource LargeButton}" />
                           <Setter Target="DeleteAllPositionDataButton.Style" Value="{StaticResource LargeButton}" />

                           <!--positions-->
                           <Setter Target="DataAvailableBlock.(Grid.Row)" Value="6"/>
                           <Setter Target="SavePositionButton.(Grid.Row)" Value="7"/>
                           <Setter Target="SendPositionTableButton.(Grid.Row)" Value="8"/>
                           <Setter Target="EditDevicePositionButton.(Grid.Row)" Value="9"/>
                           <Setter Target="DeleteAllPositionDataButton.(Grid.Row)" Value="10"/>

                           <Setter Target="GPSARadioButton.(Grid.Column)" Value="0" />
                           <Setter Target="GPSARadioButton.(Grid.Row)" Value="0" />
                           <Setter Target="GPSAPositionView.(Grid.Column)" Value="0"/>
                           <Setter Target="GPSAPositionView.(Grid.Row)" Value="1"/>

                           <Setter Target="UbloxRadioButton.(Grid.Column)" Value="0" />
                           <Setter Target="UbloxRadioButton.(Grid.Row)" Value="2"/>
                           <Setter Target="UbloxPositionView.(Grid.Column)" Value="0"/>
                           <Setter Target="UbloxPositionView.(Grid.Row)" Value="3"/>

                           <Setter Target="ManualRadioButton.(Grid.Column)" Value="0" />
                           <Setter Target="ManualRadioButton.(Grid.Row)" Value="4"/>
                           <Setter Target="ManualPositionView.(Grid.Column)" Value="0"/>
                           <Setter Target="ManualPositionView.(Grid.Row)" Value="5"/>


                        </VisualState.Setters>
                     </VisualState>
                     <VisualState x:Name="HorizontalState">
                        <VisualState.StateTriggers>
                           <AdaptiveTrigger MinWindowWidth="641"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                           <!--styles-->
                           <Setter Target="EmplacementViewGrid.Style" Value="{StaticResource HorizontalStateGrid}"/>
                           <Setter Target="SavePositionButton.Style" Value="{StaticResource LargeSquareButton}" />
                           <Setter Target="SendPositionTableButton.Style" Value="{StaticResource LargeSquareButton}" />
                           <Setter Target="EditDevicePositionButton.Style" Value="{StaticResource LargeSquareButton}" />
                           <Setter Target="DeleteAllPositionDataButton.Style" Value="{StaticResource LargeSquareButton}" />
                           <!--positions-->
                           <Setter Target="SavePositionButton.(Grid.Row)" Value="5"/>
                           <Setter Target="SavePositionButton.(Grid.Column)" Value="0"/>
                           <Setter Target="SendPositionTableButton.(Grid.Row)" Value="5"/>
                           <Setter Target="SendPositionTableButton.(Grid.Column)" Value="1"/>
                           <Setter Target="EditDevicePositionButton.(Grid.Row)" Value="5"/>
                           <Setter Target="EditDevicePositionButton.(Grid.Column)" Value="2"/>
                           <Setter Target="DeleteAllPositionDataButton.(Grid.Row)" Value="5"/>
                           <Setter Target="DeleteAllPositionDataButton.(Grid.Column)" Value="3"/>

                           <Setter Target="DeviceIdTextBox.(Grid.Column)" Value="1"/>
                           <Setter Target="DeviceIdTextBox.(Grid.Row)" Value="0"/>

                           <Setter Target="GPSARadioButton.(Grid.Column)" Value="0" />
                           <Setter Target="GPSAPositionView.(Grid.Row)" Value="3" />
                           <Setter Target="GPSAPositionView.(Grid.Column)" Value="0" />

                           <Setter Target="UbloxRadioButton.(Grid.Column)" Value="2" />
                           <Setter Target="UbloxPositionView.(Grid.Column)" Value="2" />
                           <Setter Target="UbloxPositionView.(Grid.Row)" Value="3" />

                           <Setter Target="ManualRadioButton.(Grid.Column)" Value="4" />
                           <Setter Target="ManualPositionView.(Grid.Column)" Value="4" />
                           <Setter Target="ManualPositionView.(Grid.Row)" Value="3" />

                           <Setter Target="GPSAPositionView.(Grid.ColumnSpan)" Value="1" />
                           <Setter Target="UbloxPositionView.(Grid.ColumnSpan)" Value="1" />
                           <Setter Target="ManualPositionView.(Grid.ColumnSpan)" Value="1" />

                           <Setter Target="DataAvailableBlock.(Grid.Row)" Value="4" />
                           <Setter Target="DataAvailableBlock.(Grid.Column)" Value="1" />
                           <Setter Target="DataAvailableBlock.(Grid.ColumnSpan)" Value="2" />
                        </VisualState.Setters>
                     </VisualState>
                  </VisualStateGroup>
               </VisualStateManager.VisualStateGroups>


               <Grid.ColumnDefinitions>

                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
                  <ColumnDefinition Width="Auto"/>
               </Grid.ColumnDefinitions>
               <Grid.RowDefinitions>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
                  <RowDefinition Height="Auto"/>
               </Grid.RowDefinitions>

               <TextBox x:Name="DeviceIdTextBox" Header="Device ID" HorizontalAlignment="Center"/>



               <!-- Insert PositionView between DeviceID/Manual Toggle and EmplacmentView's buttons -->
               <RadioButton x:Name="GPSARadioButton" Content=" GPSA"  Checked="GPSARadio_Checked"/>

               <views:PositionView x:Name="GPSAPositionView" Latitude="{Binding GPSALatitude}" Longitude="{Binding GPSALongitude}" 
                           Elevation="{Binding GPSAElevation}" MGRS="{Binding GPSAMGRS}" SatelliteCount="{Binding GPSASatelliteCount}" HorizontalAlignment="Stretch" >
               </views:PositionView>

               <RadioButton x:Name="UbloxRadioButton" Content="Ublox GPS"/>

               <views:PositionView x:Name="UbloxPositionView" Latitude="{Binding UbloxLatitude}" Longitude="{Binding UbloxLongitude}" 
                           Elevation="{Binding UbloxElevation}" MGRS="{Binding UbloxMGRS}" SatelliteCount="{Binding UbloxSatelliteCount}" HorizontalAlignment="Stretch"  >
               </views:PositionView>

               <RadioButton x:Name="ManualRadioButton" Content="Manual Input"/>

               <views:PositionView x:Name="ManualPositionView" Latitude="{Binding ManualLatitude}" Longitude="{Binding ManualLongitude}" 
                           Elevation="{Binding ManualElevation}" MGRS="{Binding ManualMGRS}" SatelliteCount="{Binding ManualSatelliteCount}" HorizontalAlignment="Stretch" >
               </views:PositionView>


               <TextBlock x:Name="DataAvailableBlock" Text="{Binding Error}"  HorizontalAlignment="Center" Foreground="DarkRed"/>

                  <Button x:Name="SavePositionButton" Click="SavePositionButton_Click" >
                     <Button.Content>
                        <TextBlock TextWrapping="WrapWholeWords" Text="Save Position"/>
                     </Button.Content>
                  </Button>
                  <Button x:Name="SendPositionTableButton" Click="SendPositionTableButton_Click" >
                     <Button.Content>
                        <TextBlock TextWrapping="WrapWholeWords" Text="Send Position Table"/>
                     </Button.Content>
                  </Button>
                  <Button x:Name="EditDevicePositionButton" Click="EditDevicePositionButton_Click" >
                     <Button.Content>
                        <TextBlock TextWrapping="WrapWholeWords" Text="Edit Device Position"/>
                     </Button.Content>
                  </Button>
                  <Button x:Name="DeleteAllPositionDataButton" Click="DeleteAllPositionDataButton_Click" >
                     <Button.Content>
                        <TextBlock TextWrapping="WrapWholeWords" Text="Delete All Position Data"/>
                     </Button.Content>
                  </Button>

               </Grid>
        </UserControl>
    </ScrollViewer>

</Page>

1 Ответ

1 голос
/ 13 мая 2019

Краткий ответ: я изменил привязки в моем UserControl с Текст = {Широта привязки, Режим = Двусторонний} на Текст = {x: Широта привязки, Режим = Двусторонний}

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...