c# UWP XAML usercotrols binding question

Paul Ryan 331 Reputation points
2022-10-17T22:48:52.71+00:00

I have a class model

namespace MUA.Models.Progress
{
public class PrRptModel : Base.NotifyChange

{
.........
public ObservableCollection<RptFldsModel> RptFld { get; set; }
.....
}

public class RptFldsModel : Base.NotifyChange
{
readonly static private string _defaultValue = "";
readonly static private FontFamily _fontS = new ("Segoe MDL2 Assets");
readonly static private FontFamily _fontA = new FontFamily("Segoe UI");
........
private string _painOnMovement = _defaultValue;
public string PainOnMovement
{
get { return _painOnMovement; }
set
{
_painOnMovement = CheckMinus(value);
OnPropertyChanged(nameof(PainOnMovement));
}
}

    private string _rom = _defaultValue;  
    public string ROM  
    {  
        get { return _rom; }  
        set  
        {  
            _rom = value;  
            OnPropertyChanged(nameof(ROM));  
        }  
    }  


    private string _myoTriggerPts = _defaultValue;  
    public string MYOTriggerPts  
    {  
        get { return _myoTriggerPts; }  
        set  
        {  
            _myoTriggerPts = CheckMinus(value);  
            OnPropertyChanged(nameof(MYOTriggerPts));  
        }  
    }  

    private string _initialVisit = _defaultValue;  
    public string InitialVisit  
    {  
        get { return _initialVisit; }  
        set  
        {  
            _initialVisit = value;  
            OnPropertyChanged(nameof(InitialVisit));  
        }  
    }  

....
}

I have a usercontroll

<UserControl
x:Class="MUA.Pages.Progress.UserCotrols.CCUC"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MUA.Pages.Progress.UserCotrols"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="800" >
<StackPanel HorizontalAlignment="Center" Margin="10">
.........
<StackPanel Orientation="Horizontal" Padding="-2,56,0,0">
<RepeatButton Tag="0" Margin="-25,60,0,0"
Content="{Binding Path=rptFldsModel.InitialVisit }"
FontFamily="Segoe UI" Style="{StaticResource FEven}" Click="RBtn_ISW" >
<RepeatButton.Projection>
<PlaneProjection RotationZ="270"/>
</RepeatButton.Projection>
</RepeatButton>
<RepeatButton Tag="0" Margin="-70,60,0,0"
Content="{Binding Path=rptTemp.RptFld[Indx].LastVisit, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
FontFamily="Segoe UI" Style="{StaticResource FOdd}" Click="RBtn_ISW" >
<RepeatButton.Projection>
<PlaneProjection RotationZ="270"/>
</RepeatButton.Projection>
</RepeatButton>
........

XAML.CS
public PrRptModel rptTemp = StaticFiles.ProgressRptNew;
public static int Indx;
int indx = 1;

    public PrRptModel prRptModel => DataContext as P;  
      
    public CCUC()  
    {  
        InitializeComponent();     
    }  

the rest is private void BtnClick(object sender, RoutedEventArgs e)

Source page
<Page
x:Class="MUA.Pages.Progress.CC.AnklePage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:MUA.Pages.Progress.CC"
xmlns:local1="using:MUA.Pages.Progress.UserCotrols"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

<ItemsControl Padding="10" >  
      
    <TextBlock Text="CC Ankle" FontSize="16" Foreground="Red" FontWeight="Bold" HorizontalAlignment="Center" />  
    <local1:HeaderUC>  
         
    </local1:HeaderUC>  
    <local1:CCUC>  
          
    </local1:CCUC>  
</ItemsControl>  

</Page>

XAML.CS
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class AnklePage : Page
{

    public AnklePage()  
    {  
        CCUC.Indx = 5;  
        this.InitializeComponent();  
    }  
}  

}

What I cannot figure out is how to set Binding Path=rptTemp.RptFld[Indx].LastVisit, Indx set so it displays the proper ROW

When I put an actual integer in place of Indx it works fine with x:Bind but not with Indx, I tried using Binding with
public static readonly DependencyProperty EStimProperty =
DependencyProperty.Register(
nameof(EStim),
typeof(string),
typeof(CCUC),
null);
public string EStim
{
get { return (string)GetValue(EStimProperty); }
set { SetValue(EStimProperty, value); }

    }  

<RepeatButton Tag="1,EStim" Margin="-70,60,0,0" Style="{StaticResource FEven}"
Content="{Binding Path=rptTemp.RptFld[Indx].EStimProperty, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"
FontFamily="Segoe MDL2 UI" Click="RBtn_BlankUnit" >
<RepeatButton.Projection>
<PlaneProjection RotationZ= "270"/>
</RepeatButton.Projection>
</RepeatButton>

Nothing shows

I am lost as what I am doing wrong

Thanks in advance for the help

Paul

Developer technologies | Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-10-18T07:10:26.53+00:00

    Hello,

    Welcome to Microsoft Q&A!

    I am lost as what I am doing wrong

    you can't use the Indx property in the Xaml Binding expression like that.

    The recommened way is just pass the target RptFldsModel object to the UserControl so the Content property of RepeatButton could directly bind to the LastVisit property of the target RptFldsModel object. Or maybe create a new ViewMode class for the UserControl which contains just the data the UserControl needs.

    If you insist on doing this in the original way. I'd suggest you to set the binding in code-behind where we could get the proper RptFldsModel object first. Like this:

    UserControl cs

      public void updateBinding(int index)   
        {  
            MyRepeatButton.DataContext = rptTemp.RptFld[index];  
    
            Binding binding = new Binding   
            {   
                Path = new PropertyPath("LastVisit"),  
                Mode = BindingMode.TwoWay,  
                UpdateSourceTrigger = UpdateSourceTrigger.LostFocus  
            };  
    
            MyRepeatButton.SetBinding(RepeatButton.ContentProperty,binding);  
        }  
    

    UserControl Xaml

     <RepeatButton x:Name="MyRepeatButton" Tag="0" Margin="-70,60,0,0"  
                              
                              FontFamily="Segoe UI" Click="RepeatButton_Click" >  
                    <RepeatButton.Projection>  
                        <PlaneProjection RotationZ="270"/>  
                    </RepeatButton.Projection>  
                </RepeatButton>  
    

    Call it in the Page:

     MyControl.updateBinding(3);  
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.