Search code examples
wpfdata-bindingivalueconverter

Converter never called when binding to Relative source


My Binding :

  <StackPanel x:Name="Ancestor">     
      <StackPanel.Resources>      
          <converters:DiceInputToVisualConverter x:Key="MyDiceInputToVisualConverter" />   

          <Style TargetType="{x:Type Ellipse}">
               <Setter Property="Visibility" Value="{Binding Path=/, Converter={StaticResource  MyDiceInputToVisualConverter},FallbackValue=Visible}"></Setter> 
          </Style>  
     <StackPanel.Resources>
     <StackPanel>
           <Canvas DataContext="{Binding Path=DataContext.Dice1,RelativeSource={RelativeSource AncestorType=StackPanel}}">                                           
                <Ellipse Canvas.Left="5" Canvas.Top="5"></Ellipse>
                <Ellipse Canvas.Left="5" Canvas.Top="20"></Ellipse>
           </Canvas>
     </StackPanel>

the DataContext :

  Ancestor.DataContext = game ;

the converter :

     public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
     {
        int dice = int.Parse(value.ToString());
        if (dice == 3)
            return Visibility.Visible;
        return Visibility.Hidden;
     }

my data source :

  public Class Game : INotifyPropertyChanged
  {
     private int dice1;
     public int Dice1
     {
         get { return dice1; }
         set
         {
             dice1 = value;
             if (PropertyChanged != null)
                 PropertyChanged(this, new PropertyChangedEventArgs("Dice1"));
        }
     }
   }

the binding is fine when i checked it with snoop the ellipse's DataContext had the desired value

but still the Converter is never called any ideas ?


Solution

  • Are you sure that you should be using Path=/? This notation means the currently selected item of the default collection view.