I try to tie the color of the ellipse to the field of the created class, but nothing comes out. What am I doing wrong? Xaml:

<Ellipse Width="5" Height="5" x:Name="Ell" Fill="{Binding elipce}" Grid.Column="0"/> 

C #:

  elipce = new SolidColorBrush(System.Windows.Media.Colors.Green); 
  • one
    As far as I know, the binding does not work for the fields. Use properties! - EvgeniyZ
  • @EvgeniyZ label text attachment to text field works great - ElRabbito
  • 2
    It will be interesting to see this binding ... - EvgeniyZ
  • And what type of property is yours? - Andrey NOP
  • 2
    So let's see. public string name; - this field (field), it is not possible to tie to the field! public string name {get; set;} public string name {get; set;} is a property and the whole binding goes to it. - EvgeniyZ

1 answer 1

So, that would not pull you a hundred times a million lines of code, I will write a simple answer ...
In the comments we have already figured out hopefully with you with the properties and fields.

I will remind

  • public string Name; - this is a field . If you attach to it, you will receive an error that the "Name property was not found."
  • public string Name {get; set;} public string Name {get; set;} is a property (Property), they are just used for working with bindings (Binding).

According to your problem

I have created this property with color, which changes in the constructor.

  • If it is created, everything is attached to it successfully (not in the error logs), then most likely you have not implemented INotifyPropertyChanged .
  • If it is created but not attached, then either you have no public access, or something is wrong with get.

Example

I will show a simple and disgraceful example of what you want (I will write clumsily, in practice it is better to implement commands and everything related to MVVM (view, viewmodel and model).

XAML markup

  • So, we will create a simple markup, which will contain 3 buttons and ellipse:

     <Grid HorizontalAlignment="Center" VerticalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Ellipse Grid.Row="0" Width="30" Height="30" Fill="{Binding Color}" Margin="0,0,0,5"/> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Click="BtnClick" Name="RedBtn" Width="60" Height="30" Content="Красный"/> <Button Click="BtnClick" Name="BlueBtn" Width="60" Height="30" Content="Синий"/> <Button Click="BtnClick" Name="GreenBtn" Width="60" Height="30" Content="Зеленый"/> </StackPanel> </Grid> 
  • As we can see here, I have already specified a color binding to nobody Color , so let's create it, but .. First, let's implement INPC (INotifyPropertyChanged):

INotifyPropertyChanged

What is INotifyPropertyChanged? In fact, it is a kind of toolkit that helps us interact with the canvas (that is, the window and its View part). In other words, using this interface, we notify our View of changes that have occurred to our properties, which eventually updates the associated controls (changes text, color, size, etc.).

For the implementation we need (by the way, there is a very good option that is described in this answer:

  • In our class with properties, we look at whether they can change and whether we need to update the interface for them, if there are such properties, then after the class name we write (after a colon) : INotifyPropertyChanged .
  • Then at least kill me, but I still don’t know who offers it, ReSharper or the studio itself, but when I point to the underlined inscription, an assistant is displayed that offers to implement the missing lines of code (most likely ReSharper), and so, the following code is created in our classroom:

     public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } 
  • Everything, our VM is ready to notify our controls about data changes.

Property creation

As we have already figured out above - WPF loves specifically to properties, so let's create it:

  • But, here we must also say that "this property has changed" and this is what the "miracle" is created for:

     private Brush color; public Brush Color { get => color; set { color = value; OnPropertyChanged(); } } 

What's going on here? See, when getting data (get) - we give the value that is stored in a private variable, and when changing the data (set) - we set our private variable the changed value and call OnPropertyChanged(); which will inform the interface about the new value. You will need to do this property wherever you need to notify the interface about the change.

All code of our VM:

 public class TestViewModel : INotifyPropertyChanged { private Brush color; public Brush Color { get => color; set { color = value; OnPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } 

That's it, I think your problem will be solved already.

We will add the application ...

  • In the main window, we specify our VM as a property:

     private TestViewModel TestVM { get; set; } = new TestViewModel(); 
  • In the MainWindow constructor, specify the DataContext:

     DataContext = TestVM; 
  • And lastly, we will clumsily change the color in our VM (we were too lazy to make a team, but we should ...):

     private void BtnClick(object sender, RoutedEventArgs e) { var btn = sender as Button; switch (btn?.Name) { case "RedBtn": TestVM.Color = new SolidColorBrush(Colors.Red); break; case "GreenBtn": TestVM.Color = new SolidColorBrush(Colors.Green); break; case "BlueBtn": TestVM.Color = new SolidColorBrush(Colors.Blue); break; } } 
  • Run and ... Result:

result

  • This is a good answer to my question, but I noticed one interesting thing. If you leave it like this , and call a method that adds an object from the main thread, everything will be fine, the circles in the list will change depending on the status entered, but the fact is that I need to call an add from another thread, add a new object here so But in this case, after getting all the User , I get it . I do not understand what is wrong. - ElRabbito
  • And the most interesting is that if I replace a piece of code that changes the color of the elipce property depending on the status , then everything is fine, everything starts, but already, of course, without circles. - ElRabbito
  • @ElRabbito The screen from the first comment is not working, I do not know what your problem is. As for the status, it’s better for me to move it to View at all (as for example here - EvgeniyZ
  • What surprises me most of all is that the text, for some reason, is normal as I am doing, but these circles are not. Here is the result of adding elements for example immediately after initialization . If you add from another thread using Dispatcher , then with uncompleted color in the constructor of the User object, the application goes into suspend mode. Why it is on this property that the light did not converge with a wedge, I do not know. Some kind of mystery. - ElRabbito