繁体   English   中英

数据绑定在Windows Phone 8中不起作用

[英]Data binding not working in windows phone 8

我试图动态更改矩形的颜色,但是当我尝试更改矩形时,什么也没有发生

<Grid x:Name="LayoutRoot">
    <Grid Name="rootgrid" Margin="0,10,0,-10">

        <Rectangle Name="box3" HorizontalAlignment="Stretch" Margin="56,500,71,182">
            <Rectangle.Fill>
                <SolidColorBrush Color="{Binding color1}" />
            </Rectangle.Fill>
        </Rectangle>
    </Grid>

前两个矩形有效,但box3不显示颜色

public Program()
{
    InitializeComponent();
    MyColors color = new MyColors();
    color.color1 = Colors.Yellow;
    box3.DataContext = new SolidColorBrush(Colors.Yellow);;
}


private SolidColorBrush _color1;

    // Declare the PropertyChanged event.
    public event PropertyChangedEventHandler PropertyChanged;

    // Create the property that will be the source of the binding.
    public SolidColorBrush color1
    {
        get { return _color1; }
        set
        {
            _color1 = value;
            NotifyPropertyChanged("color1");
        }
    }

    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
    }

上面没有抛出任何错误,但是它改变了box3的填充值,我不确定自己在做什么错。 任何帮助将不胜感激。

更新了代码以反映更改

更新:解决了问题是Visual Studio无法正确更新,该代码在7.1中有效,但在8.0中无效

我认为问题是矩形的边距。 将“填充”更改为xaml中的某些静态颜色。 看看是否可以看到它。 我已经测试过了,没有显示出来。 所以,我改变了它。 而且我认为您必须将Fill设置为SolidColorBrush ,而不是Color。 同时从.xaml中删除DataContext。

第一个解决方案:

和ViewModel:

 public class MyColors : INotifyPropertyChanged
    {
        private SolidColorBrush _color1;

        // Declare the PropertyChanged event.
        public event PropertyChangedEventHandler PropertyChanged;

        // Create the property that will be the source of the binding.
        public SolidColorBrush color1
        {
            get { return _color1; }
            set
            {
                _color1 =value;
                NotifyPropertyChanged("color1");
            }
        }

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,
                    new PropertyChangedEventArgs(propertyName));
            }
        }
    }

和MainPage构造函数:

    InitializeComponent();
    MyColors color = new MyColors();
    color.color1 = new SolidColorBrush(Colors.Yellow);
    box3.DataContext = color;

第二种解决方案(更好):

但是,如果您仍然想使用Color,则可以通过xaml进行如下更改:

<Rectangle Name="box3" HorizontalAlignment="Stretch" Margin="56,500,71,182">
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding color1, Mode=OneWay}" />
    </Rectangle.Fill>
</Rectangle>">

您已经设置了两次DataContext ,一次在Xaml中,一次在代码中,并且FillBrush而不是Color您必须设置Fill的color属性

<Rectangle Name="box3" HorizontalAlignment="Stretch" Margin="56,500,71,182">
    <Rectangle.Fill>
        <SolidColorBrush Color="{Binding color1}" />
    </Rectangle.Fill>
</Rectangle>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM