Skip to content

Commit

Permalink
Merge pull request #2279 from unoplatform/dev/erli/update-csm-name-me…
Browse files Browse the repository at this point in the history
…thod

docs: Add CSM CallBack usage to Name method
  • Loading branch information
dansiegel authored Jun 12, 2024
2 parents 952726f + 804aa65 commit c2cd547
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 24 deletions.
53 changes: 50 additions & 3 deletions doc/Learn/Markup/Appendix/AccessingControlInstance.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ uid: Uno.Extensions.Markup.Appendix.AccessingControlInstance

# Accessing a Control Instance

There are a number of times where you may find yourself wanting to access the instance of a control that you initialized. This could be useful for bindings or even to later configure Event Handlers on the control. You can most easily do this with the `Assign` method as follows:
There are a number of times when you may find yourself wanting to access the instance of a control that you initialized. This could be useful for bindings or even to later configure Event Handlers on the control. You can most easily do this with the `Name` method as follows:

```cs
```csharp
this.Content(
new StackPanel()
.Children(
new Button()
.Assign(out var button)
.Name(out var button)
.Content("Press Me")
)
);
Expand All @@ -21,3 +21,50 @@ button.Click += delegate {
button.Content = $"Clicked {i++} times";
}
```

You can also define a `delegate` using the `Name` method:

```csharp
int i = 1;

this.Content(
new StackPanel()
.Children(
new Button()
.Name(button =>
{
button.Click += (s, e) =>
{
button.Content = $"Clicked {i++} times";
};
})
.Content("Press Me")
)
);
```

Or if you want to expose a variable and also define a `delegate`:

```csharp
int i = 1;

this.Content(
new StackPanel()
.Children(
new Button()
.Name(out var button, (b) =>
{
b.Click += (s, e) =>
{
b.Content = $"Clicked {i++} times";
};
})
.Content("Press Me")
)
);

var buttonContent = button.Content;
```

> [!NOTE]
> Using the `.Name(out var button)` or `.Name(button => { ... })` syntax not only gives you a variable representing your `FrameworkElement` control but also sets the control's `Name` property to match the variable name. For example, in the scenario mentioned earlier, the `Button` would have its `Name` property set to "button" because the variable name used is `button`.
13 changes: 1 addition & 12 deletions doc/Learn/Markup/DependencyPropertyBuilder.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ If you wish to perform tasks such as manipulate the value of the binding or form

### Reference Sources

Sometimes you aren't binding to the `DataContext` of the element and instead, you need to reference another source. With WinUI we have 2 ways of doing this. The first is that we could specify a source directly such as:
Sometimes you aren't binding to the `DataContext` of the element and instead, you need to reference another source:

```cs
new Slider().Name(out var slider),
Expand All @@ -58,17 +58,6 @@ new TextBlock()
> [!NOTE]
> Using the `.Name(out var fe)` syntax not only gives you a variable representing your `FrameworkElement` control but also sets the control's `Name` property to match the variable name. For example, in the scenario mentioned earlier, the `Slider` would have its `Name` property set to "slider" because the variable name used is `slider`.

The second is that we can leverage the element name for our binding such as the following:

```cs
new TextBlock()
.Text(x => x
.Source("slider")
.Binding(() => slider.Value)),
new Slider().Name("slider")
```

## Additional Reading

- [Binding 101](xref:Uno.Extensions.Markup.Binding101)
Expand Down
8 changes: 4 additions & 4 deletions doc/Learn/Markup/HowTo-CustomMarkupProject.md
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ In the ViewModel, the Model's data is already loaded into the Instance of the cl
.Text(Title)
.FontSize(18)
.FontWeight(FontWeights.Bold)
.Assign(out var searchText),
.Name(out var searchText),
new TextBlock()
.Text(SubTitle)
.FontSize(16)
Expand Down Expand Up @@ -1335,7 +1335,7 @@ In the ViewModel, the Model's data is already loaded into the Instance of the cl
.Text(Title)
.FontSize(18)
.FontWeight(FontWeights.Bold)
.Assign(out var searchText),
.Name(out var searchText),
new TextBlock()
.Text(SubTitle)
.FontSize(16)
Expand Down Expand Up @@ -1607,7 +1607,7 @@ In the ViewModel, the Model's data is already loaded into the Instance of the cl
.Text(Title)
.FontSize(18)
.FontWeight(FontWeights.Bold)
.Assign(out var searchText),
.Name(out var searchText),
new TextBlock()
.Text(SubTitle)
.FontSize(16)
Expand Down Expand Up @@ -1986,7 +1986,7 @@ In the ViewModel, the Model's data is already loaded into the Instance of the cl
.Text(Title)
.FontSize(18)
.FontWeight(FontWeights.Bold)
.Assign(out var searchText),
.Name(out var searchText),
new TextBlock()
.Text(SubTitle)
.FontSize(16)
Expand Down
6 changes: 3 additions & 3 deletions doc/Learn/Markup/SourceUsage.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Binding a property directly from one UI element to another is a common scenario.
new StackPanel()
.Children(
new Button()
.Assign(out var button)
.Name(out var button)
.Content("I am a button"),

// Creating a TextBox and binding its Text property to a Button's Content
Expand All @@ -27,7 +27,7 @@ new StackPanel()
new StackPanel()
.Children(
new TextBox()
.Assign(out var textBox)
.Name(out var textBox)
.Text("I am a TextBox"),

// Creating a Button and binding its Content property to the TextBox's Text property with TwoWay binding
Expand All @@ -49,7 +49,7 @@ new TextBox()
new StackPanel()
.Children(
new Button()
.Assign(out var button)
.Name(out var button)
.Content("I am a button"),

// Binding to a property of a DataContext
Expand Down
4 changes: 2 additions & 2 deletions doc/Learn/Markup/VisualStateManager.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ When updating the children of a given control, it is important to ensure that th
```cs
new Grid()
.Children(
new TextBlock().Assign(out var textBlock)
new TextBlock().Name(out var textBlock)
)
.VisualStateManager(vsm => vsm
.Group(group => group
Expand All @@ -36,7 +36,7 @@ In addition to adding various style setters to a given state, it is also possibl
```cs
new Grid()
.Children(
new TextBlock().Assign(out var textBlock)
new TextBlock().Name(out var textBlock)
)
.VisualStateManager(vsm => vsm
.Group(group => group
Expand Down

0 comments on commit c2cd547

Please sign in to comment.