简体   繁体   English

绑定值无法正常工作

[英]Binding value don't work correctly

I have code: 我有代码:

<ColorAnimationUsingKeyFrames Storyboard.TargetProperty="(Panel.Background).   (GradientBrush.GradientStops)[1].(GradientStop.Color)" Storyboard.TargetName="grid">
    <EasingColorKeyFrame KeyTime="0:0:0.5" Value="{Binding CorBackground}"/>
</ColorAnimationUsingKeyFrames>

color value is defined as Value="#FFFFFF" , I'll want to define this color using binding. 颜色值定义为Value="#FFFFFF" ,我将使用绑定来定义此颜色。 Value="{Binding CorBackground} . It's possible? Value="{Binding CorBackground} ,这可能吗?

I create a property in MainWindow.xaml.cs , but don't work: 我在MainWindow.xaml.cs创建了一个属性,但是不起作用:

private string _corBackground = string.Empty;
public string CorBackground
{
    get { return _corBackground; }
    set { _corBackground = value; }
}

If you don't want to use a MVVM-pattern with proper binding, a possible solution is to use a DependecyProperty in your UserControl (as in the example a SolidColorBrush Property called "BackgroundColor"). 如果您不想使用具有适当绑定的MVVM模式,则可能的解决方案是在UserControl中使用DependecyProperty (如示例中称为“ BackgroundColor”的SolidColorBrush属性)。 Your MainWindow.cs would look like 您的MainWindow.cs看起来像

    public partial class MainWindow : Window
{
    public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register(
        "BackgroundColor", typeof (SolidColorBrush), typeof (MainWindow), new PropertyMetadata(default(SolidColorBrush)));

    public SolidColorBrush BackgroundColor
    {
        get { return (SolidColorBrush) GetValue(BackgroundColorProperty); }
        set { SetValue(BackgroundColorProperty, value); }
    }

    public MainWindow()
    {
        BackgroundColor = new SolidColorBrush(Colors.LightBlue);
        InitializeComponent();
    }

    private void ChangeBackgroundButton_OnClick(object sender, RoutedEventArgs e)
    {
        BackgroundColor = new SolidColorBrush(Colors.CornflowerBlue);
    }
}

Where your Grid Background Property is bound to "BackgroundColor". 网格背景属性绑定到“ BackgroundColor”的位置。

<Window x:Class="ListBoxStackoverflow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525"
    x:Name="WindowsWithDependencyProperty">

<Grid Background="{Binding ElementName=WindowsWithDependencyProperty, Path=BackgroundColor}">
    <Button x:Name="ChangeBackgroundButton" 
            HorizontalAlignment="Left" VerticalAlignment="Top"
            Click="ChangeBackgroundButton_OnClick">Change Background</Button>
</Grid>

Note that this is just one possible solution. 请注意,这只是一种可能的解决方案。

I am not sure if this property can be a bindable one but normally it should be. 我不确定此属性是否可以绑定,但通常应该是可以绑定的。 If so, you have to set the DataContext of the control to an instance of the class that contains your property. 如果是这样,则必须将控件的DataContext设置为包含您的属性的类的实例。

If it is in the same control/window/class then a simple this.DataContext = this in the constructor or in the Loaded event of your control/window whatever will do the job for you. 如果它在相同的控件/窗口/类中,则在构造器中或控件/窗口的Loaded事件中使用简单的this.DataContext = this可以为您完成此工作。

The class needs to implement INotifyPropertyChanged, and the property needs to raise the PropertyChanged event in the set method for the binding to work properly. 该类需要实现INotifyPropertyChanged,并且该属性需要在set方法中引发PropertyChanged事件,以使绑定正常工作。

Also make sure your object has the data context set to the code behind. 还要确保您的对象的数据上下文设置为后面的代码。 One way to do this is in the xaml window declaration. 一种方法是在xaml窗口声明中。 Set the data context like this: 像这样设置数据上下文:

DataContext={Binding RelativeSource={RelativeSource.Self}} DataContext = {绑定RelativeSource = {RelativeSource.Self}}

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

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