Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

question. bind to array works ? #21

Open
cesarchefinho opened this issue Mar 30, 2023 · 4 comments
Open

question. bind to array works ? #21

cesarchefinho opened this issue Mar 30, 2023 · 4 comments

Comments

@cesarchefinho
Copy link

Does this works with compiledbindings ?

// ViewModel

public string ErrorOf [string index]
{
// get and set accessors
}

// XAML

< textblock Text={x:Bind ViewModel.ErrorOf ["Username"]}

if yes, please considere to add binding to arrays in readme, if no please considere open an issue

UWP Bind supports bind to array property

@levitali
Copy link
Owner

Yes, you can access elements of an array or of the Item property.

The c# and xaml code in your example is not correct. You can't have named Indexer property in c#. Instead you can have something like this:

public class ErrorsCollection
{
   public string this[string index]
   {
   	get => // some logic
   }
}

class ViewModel
{
   public ErrorsCollection ErrorOf { get; }
}
<TextBlock Text="{x:Bind ViewModel.ErrorOf['Username']}"/>

@cesarchefinho
Copy link
Author

and what about IndexerName attribute ?

UWP and WINUI recognize it in x:Bind

class ViewModel
{
....
[IndexerName("ErrorOf")]
public string this[string columnName] =>
string.Join(Environment.NewLine,
(from ValidationResult e
in GetErrors(columnName)
select e.ErrorMessage));

public void OnErrorsChanged(object sender, DataErrorsChangedEventArgs e)
{
    if (string.IsNullOrWhiteSpace(e.PropertyName))
    {
        OnPropertyChanged("ErrofOf['']");
    }
    else
    {
        OnPropertyChanged($"ErrorOf['{e.PropertyName}']");
    }
}

@cesarchefinho
Copy link
Author

Does compiled bindings recognize [IndexerName] attribute ?

@levitali
Copy link
Owner

levitali commented Apr 8, 2023

IndexerNameAttribute currently is not supported. I'll add it in the next version. Now the property with "Item" name is searched to find the indexer property.

IndexerNameAttribute is actually meant to be able to consume c# libraries in other programming languages. If your code is completely in c#, setting the attribute brings you nothing. You can’t use the name neither in standard Bindings nor in UWP/WinUI x:Binds.

In your OnErrorsChanged you have to raise PropertyChanged event with empty string.

For this ViewModel with indexer:

	public class MainViewModel : IDictionary<string, string>, INotifyPropertyChanged
	{
		[IndexerName("ErrorOf")]
		public string this[string index] { get => ...

And for this XAML in UWP:

<Grid>
        <TextBlock Text="{x:Bind _viewModel['a'], Mode=OneWay}"/>
        <TextBlock Text="{x:Bind _viewModel['b'], Mode=OneWay}"/>
    </Grid>

this PropertyChanged handler code is generated:´

public void PropertyChanged__viewModel(object sender, global::System.ComponentModel.PropertyChangedEventArgs e)
                {
                    MainPage_obj1_Bindings bindings = TryGetBindingObject();
                    if (bindings != null)
                    {
                        string propName = e.PropertyName;
                        global::App7.MainViewModel obj = sender as global::App7.MainViewModel;
                        if (global::System.String.IsNullOrEmpty(propName))
                        {
                            if (obj != null)
                            {
                                bindings.Update__viewModel_K372029373(obj["a"], DATA_CHANGED);
                                bindings.Update__viewModel_K372029376(obj["b"], DATA_CHANGED);
                            }
                        }
                        else
                        {
                            switch (propName)
                            {
                                default:
                                    break;
                            }
                        }
                    }
                }

In my CompiledBindings notifications for element access expressions are currently not supported. I will also implement it in the next version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants