eoweww
eoweww

Reputation: 117

Two Data binding in same control WPF

When I wrote "Admin" in textbox I want to change backcolor of label that its content is binding to class propery as in the below:

<Window.Resources>
    <local:TextToColorConverter x:Key="TextToColorConverterDataSource" d:IsDataSource="True"/>
    <local:Class1 x:Key="Class1DataSource" d:IsDataSource="True"/>
</Window.Resources>
<Grid DataContext="{Binding Source={StaticResource Class1DataSource}}">
    <Label x:Name="label" Content="{Binding FullName, Mode=OneWay}" Height="26.463" Margin="77.951,23.512,232.463,0" VerticalAlignment="Top" Background="{Binding Content, Converter={StaticResource TextToColorConverterDataSource},UpdateSourceTrigger=PropertyChanged}"/>
    <TextBox x:Name="textBox1" Height="29.878" Margin="77,80,200,0" TextWrapping="Wrap" Text="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" HorizontalAlignment="Left"/>
</Grid>

As I said above label content is binding to a class propery also textbox text is binding to class property.The code is:

class Class1 : INotifyPropertyChanged
    {

        #region Properties
        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                OnPropertyChanged("Name");
                OnPropertyChanged("FullName");
            }
        }


        private string _fullname;
        public string FullName
        {
            get { return string.Format("{0}", _name); }
            set
            {
                _fullname = value;
                OnPropertyChanged("FullName");
            }
        }
        #endregion

        #region INotifyPropertyChanged Implementing
        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion

    }

Label's background is binding to another class and it is converting text to color:

   public class TextToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value.ToString() == "Admin")
                return new SolidColorBrush(Colors.Yellow);
            else
                return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Label content data binding is working well but background data binding not working...(When I binding to textbox's text to label background is working but I wondered How can I make this way)

Upvotes: 0

Views: 467

Answers (1)

Funk
Funk

Reputation: 11201

The problem is with the Background Binding

Background="{Binding Content, Converter= ...}"

As written, you're binding to a property Content on the Grid's DataContext, which doesn't exist.

You could either bind to the right property on the Grid's DataContext:

Background="{Binding FullName, Converter= ...}"

Or bind to the Content property on the Label using RelativeSource:

Background="{Binding Content, RelativeSource={RelativeSource Self}, Converter= ...}"

Upvotes: 1

Related Questions