2013-04-14 42 views
0

我有一个XAML UserControl,它定义了一个相当基本的按钮 - 我想定义一个Bindable属性HasMeasurements,当HasMeasurements为false时,将显示重叠图像。但是,当我将它包含在我的项目中并将其绑定到ViewModel时,它不会一致地更新。绑定不能在Windows Phone 7自定义用户控件上工作

我相信ViewModel正确地通知绑定,因为我已经将同一个ViewModel属性同时绑定到另一个单独的元素,并且按预期进行更新。当我更新模拟数据时它也适用于Blend。

我试过this solution,它定义了一个回调函数,我用编程方式改变可视性,但是这个回调函数在每次ViewModel属性改变时都不会被调用,只是有时候。我也尝试使用this solution绑定XAML中的可见性和非依赖项属性,该属性也无效。我也曾尝试推行NotifyPropertyChanged出于绝望,但没有运气要么...

这里是我的XAML,

<UserControl x:Class="MyApp.View.Controls.ConversionBtn" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
    mc:Ignorable="d" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}" 
    FontSize="{StaticResource PhoneFontSizeNormal}" 
    Foreground="{StaticResource PhoneForegroundBrush}" 
    d:DesignHeight="480" d:DesignWidth="480"> 

    <Grid x:Name="LayoutRoot" Background="{StaticResource PhoneChromeBrush}"> 

     <Grid x:Name="btnGrid" toolkit:TiltEffect.IsTiltEnabled="True" Height="115"> 
      <Border Background="{StaticResource ImgOverlayColor}" BorderThickness="0" Padding="0" VerticalAlignment="Top" > 
       <TextBlock x:Name="titleTxtBlock" FontSize="{StaticResource PhoneFontSizeMedium}" Foreground="{StaticResource TileTxtColor}" Margin="6,0,0,0"/> 
      </Border> 
      <Image x:Name="notAvailableImg" Source="/Images/ConversionNotAvailableOverlay.png" HorizontalAlignment="Center" VerticalAlignment="Center" Stretch="None" /> 
     </Grid> 

    </Grid> 
</UserControl> 

这里是后面的代码,

// usings here ... 

namespace MyApp.View.Controls 
{ 
    public partial class ConversionBtn : UserControl 
    { 
     public ConversionBtn() 
     { 
      InitializeComponent(); 

      if (!TiltEffect.TiltableItems.Contains(typeof(ConversionBtn))) 
       TiltEffect.TiltableItems.Add(typeof(ConversionBtn)); 

      //this.DataContext = this; 
     }  

     public string Title 
     { 
      get { return this.titleTxtBlock.Text; } 
      set { this.titleTxtBlock.Text = value; } 
     } 

     public static readonly DependencyProperty HasMeasurementsProperty = 
      DependencyProperty.Register("HasMeasurements", typeof(bool), typeof(ConversionBtn), 
      new PropertyMetadata(false, new PropertyChangedCallback(HasMeasurementsPropertyChanged))); 

     private static void HasMeasurementsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 
     { 
      ConversionBtn cBtn = (ConversionBtn)d; 
      bool val = (bool)e.NewValue; 
      if (val) 
      { 
       cBtn.notAvailableImg.Visibility = Visibility.Collapsed; 
      } 
      else 
      { 
       cBtn.notAvailableImg.Visibility = Visibility.Visible; 
      } 
      cBtn.HasMeasurements = val; 

     } 

     public bool HasMeasurements 
     { 
      get { return (bool)GetValue(HasMeasurementsProperty); } 
      set { SetValue(HasMeasurementsProperty, value); } 
     } 
    } 
} 

回答

1

你有一个回调,这是HasMeasurment propetry被改变后调用。

而在回调中,您再次更改它。所以,你有一个合乎逻辑的误会。

如果你需要用这个值做点什么 - 只要把它保存在私人领域。

private static void HasMeasurementsPropertyChanged(DependencyObject d, 
DependencyPropertyChangedEventArgs e) 
       { 
        ConversionBtncBtn = (ConversionBtn)d; 
        bool val = (bool)e.NewValue; 
        if (val) 
        { 
         cBtn.notAvailableImg.Visibility = Visibility.Collapsed; 
        } 
        else 
        { 
         cBtn.notAvailableImg.Visibility = Visibility.Visible; 
        } 
        cBtn.SetMeasurments(val); 

       } 

      private bool measurmentsState; 
      public void SetMeasurments(bool value) 
      { 
       measurmentsState = value; 
      } 

Here您可以通过CHARLS Petzold的有关Windows Phone开发得到免费的电子书,有大约依赖属性一个不错的篇章。

+0

感谢您的回应,你说什么是有道理的,(我正在复制[在这篇文章中的顶部答案]](http://stackoverflow.com/questions/6808670/windows-phone-7-silverlight-user-control - 数据绑定 - 不工作,在定制-PRO))。但是,删除该行似乎没有区别... – Brendan

0

Ah damnit,这是Anton的回答和我没有将图像设置为“内容”的事实的组合,因此它在Blend中加载,但未出现在部署的应用程序中。

相关问题