0

我有一个 XAML UserControl,它定义了一个相当基本的按钮 - 我想定义一个 Bindable 属性,该属性将在为 falseHasMeasurements时显示覆盖图像。HasMeasurements但是,当我将它包含在我的项目中并将其绑定到 ViewModel 时,它不会持续更新。

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

我已经尝试过这个解决方案,它定义了一个回调,我以编程方式更改可见性,但是每次 ViewModel 属性更改时都不会调用此回调,只是有时。我还尝试使用此解决方案和一个也不起作用的非依赖属性绑定 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); }
        }
    }
}
4

2 回答 2

1

您有一个回调,在HasMeasurment属性更改后调用。

并在回调中再次更改它。所以,你有一个逻辑错误。

如果您需要使用此值执行某些操作 - 只需将其保存在私有字段中。

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

在这里,您可以获取 Charls Petzold 撰写的关于 Windows Phone 开发的免费电子书,其中有一章很好地介绍了 Dependency Properties。

于 2013-04-14T16:45:50.237 回答
0

啊该死,这是安东的回答和我没有将我的图像设置为“内容”这一事实的结合,因此它在 Blend 中加载但在部署的应用程序中不存在。

于 2013-04-14T17:34:23.510 回答