0

I made an UserControl that contains three toggle buttons that are each bound to different items of the same ObservableCollection. The UserControl is later used inside a Popup.

For some reason, when I toggle one of them, on an other instance of the UserControl within an other popup, it is also toggled.

The three ToggleButtons inside the UserControl look like this :

<DockPanel DockPanel.Dock="Top" LastChildFill="False" Margin="0, 0, 0, 8">
    <TextBlock DockPanel.Dock="left" Margin="0" Text="ROTATION" Foreground="{StaticResource BaseText}" VerticalAlignment="Center"/>
    <ToggleButton DockPanel.Dock="Right" Height="25" Width="25" Tag="3" IsChecked="{Binding RotationAxis[2], ElementName=RotationPanel,  Converter={StaticResource BoolToString}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" Click="ToggleButton_Click"  Content="Z"/>
    <ToggleButton IsChecked="{Binding RotationAxis[1], ElementName=RotationPanel,  Converter={StaticResource BoolToString}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
    <ToggleButton IsChecked="{Binding RotationAxis[0], ElementName=RotationPanel,  Converter={StaticResource BoolToString}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</DockPanel>

They are bound to this DependencyProperty :

public static readonly DependencyProperty RotationAxisProperty =
DependencyProperty.Register("RotationAxis", typeof(ObservableCollection<string>), typeof(RotationSelector), new PropertyMetadata(new ObservableCollection<string> { "True", "True", "True" }));
[Bindable(true)]
public ObservableCollection<string> RotationAxis
{
    get { return (ObservableCollection<string>)this.GetValue(RotationAxisProperty); }
    set { this.SetValue(RotationAxisProperty, value); }
}

The UserControl is then used twice in my app :

<Popup x:Name="SpritePopupRotation">
    <CMiX:RotationSelector x:Name="SpriteSelectedRotation" RotationAxis="{Binding Datacontext.SpriteRotationAxis, ElementName=Ch_Parameters, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" AxisChanged="RotationSelector_AxisChanged"/>
</Popup>

<Popup x:Name="MaskPopupRotation">
    <CMiX:RotationSelector x:Name="MaskSelectedRotation" RotationAxis="{Binding Datacontext.MaskRotationAxis, ElementName=Ch_Parameters, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" AxisChanged="RotationSelector_AxisChanged"/>
</Popup>

Now, when I start my app, the three ToggleButtons from one UserControl look like they are bound to the other three of the other instance of the UserControl.

Why is this happening ?

lecloneur
  • 424
  • 5
  • 20
  • 2
    Please provide us with a [mcve]. There are a lot of unneeded things in your example that make it hard to read. Furthermore, `Datacontext` is not `DataContext`, and we don't see your view-models. – dymanoid May 02 '18 at 15:31
  • sure but what is the unneeded things you're talking about ? and in what "complete" way this shoud be ? – lecloneur May 02 '18 at 15:36
  • 1
    We don't need in the XAML markup the widths, heights, styles, tags, event handlers, not relevant bindings etc. They are not important for your issue but just bloat the whole markup with unneeded info. – dymanoid May 02 '18 at 15:39
  • I see, sorry about that, I removed all unnecessary XAML. You were right about the Datacontext / DataContext, I didn't see and no error anywhere. Thank you for your help. – lecloneur May 02 '18 at 15:43
  • Problem was : I wrote Datacontext, while it should have been "DataContext". But I didn't notice since I'm working for quite a while and no errors were thrown. – lecloneur May 02 '18 at 15:48
  • Stupid question I know: Did you make sure that MaskRotationAxis and SpriteSelectedRotation are not the identical object? – Lumo May 04 '18 at 10:30
  • You **should not** provide default value for dependency properties that hold collections. The provided value is not created for each control separately, but is a single static instance shared by all the controls (hence your results). If you want the initial value to no be `null` set it in the control's constructor. See [Collection-Type DependencyProperties](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/collection-type-dependency-properties). – Grx70 May 25 '18 at 08:28
  • Possible duplicate of [Multiple user controls share collection dependency property](https://stackoverflow.com/questions/3214839/multiple-user-controls-share-collection-dependency-property) – Grx70 May 25 '18 at 08:29

0 Answers0