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:

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