绑定到相关源时,从不调用转换器

时间:2012-03-07 15:50:52

标签: wpf data-binding ivalueconverter

我的约束力:

  <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>

DataContext:

  Ancestor.DataContext = game ;

转换器:

     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;
     }

我的数据来源:

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

当我用snoop检查它时,绑定很好,椭圆的DataContext具有所需的值

但转换器从未被称为任何想法?

2 个答案:

答案 0 :(得分:2)

您确定要使用Path=/吗?此表示法表示默认集合视图的当前选定项目。

答案 1 :(得分:0)

我看到你调用了一个静态资源

StaticResource MyDiceInputToVisualConverter

您在哪里宣布此资源。例如,在Window.Resources部分中,如下所示。

<Window.Resources>
    <!-- Converters that are used on the MainWindow. -->
    <Converters:MyDiceInputToVisualConverter x:Key="MyDiceInputToVisualConverter" />
</Window.Resources>