2

I have a IMultivalueConverter which updates the background color of a StackPanel when PropertyA or PropertyB is changed. These Controls are created dynamically.

Problem: I have added two StackPanels and changed the PropertyA in the code when a button is clicked. This leads to a property changed event.

private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
   if (this.PropertyChanged != null)
   {
     this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
   }
}

For the first stackpanel the background color is not updated, but for the second stackpanel this.PropertyChanged inturn calls my MultiValueConverter and background color is updated.

I am not able to understand why only one control is getting updated when both belong to same type and eventhandler is not null.

EDIT: If I drag and drop a cell value from other control (DevExpress DataGrid) into the first stackpanel and then change the property, the background is not getting updated. It works fine until I drag and drop.

Update:

 <StackPanel.Background>
   <MultiBinding Converter="{StaticResource ResourceKey=BackgroundColorConverter}">
         <Binding Path="PropertyA" UpdateSourceTrigger="PropertyChanged" />
         <Binding Path="PropertyB" UpdateSourceTrigger="PropertyChanged" />
  </MultiBinding>
</StackPanel.Background>

Update 2: I have also tried using MultiDataTrigger instead of Converter, but couldn't solve the problem.

lerner1225
  • 862
  • 7
  • 25

2 Answers2

0

Unless i miss understood you, i don't see any complication in doing that,

 <Window.Resources>
        <app:BackgroundColorConverter x:Key="BackgroundColorConverter"/>
 </Window.Resources>
 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Orientation="Horizontal" >
        <TextBox Text="{Binding PropertyA}" Width="200"/>
        <TextBox Text="{Binding PropertyB}" Width="200"/>
    </StackPanel>
    <StackPanel Grid.Row="1" Margin="5">
        <StackPanel.Background>
            <MultiBinding Converter="{StaticResource ResourceKey=BackgroundColorConverter}">
                <Binding Path="PropertyA" UpdateSourceTrigger="PropertyChanged" />
                <Binding Path="PropertyB" UpdateSourceTrigger="PropertyChanged" />
            </MultiBinding>
        </StackPanel.Background>
    </StackPanel>
    <StackPanel Grid.Row="2" Margin="5">
        <StackPanel.Background>
            <MultiBinding Converter="{StaticResource ResourceKey=BackgroundColorConverter}">
                <Binding Path="PropertyA" UpdateSourceTrigger="PropertyChanged" />
                <Binding Path="PropertyB" UpdateSourceTrigger="PropertyChanged" />
            </MultiBinding>
        </StackPanel.Background>
    </StackPanel>
</Grid>

the Converter :

    public class BackgroundColorConverter:IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        if (values==null)
        {
            return null;
        }
        return
            new SolidColorBrush(Color.FromRgb(byte.Parse(values[0].ToString()), byte.Parse(values[1].ToString()),
                50));
    }

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

..And the Code behind

    public partial class MainWindow : Window,INotifyPropertyChanged
{
    private byte _propertyA ;
    private byte _propertyB;

    public byte PropertyA
    {
        get
        {
            return _propertyA;
        }

        set
        {
            if (_propertyA == value)
            {
                return;
            }

            _propertyA = value;
            OnPropertyChanged();
        }
    }

    public byte PropertyB
    {
        get
        {
            return _propertyB;
        }

        set
        {
            if (_propertyB == value)
            {
                return;
            }

            _propertyB = value;
            OnPropertyChanged();
        }
    }
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

let me know if i did miss something

SamTh3D3v
  • 9,854
  • 3
  • 31
  • 47
  • Hi Sam, U haven't misunderstood. I have edited the question. The update is lost when I drag and drop into the stackpanel. – lerner1225 Jul 20 '15 at 08:44
0

Reason:

When a value is dragged over the StackPanel, I am setting the BackgroundColor manually.

stackpanel.BackGround = new SolidColorBrush(Color.FromArgb(255,255,255,141));

Solution:

When I commented this line, the MultiValue converter is called and BackGround color is updated properly. I created a property which changes according to DragEnter, DragOver and DragLeave events and then converter is called, I evaluate this value and set the Background color in the converter.

lerner1225
  • 862
  • 7
  • 25