如何在Silverlight中根据绑定值改变对象的颜色?

发布于 2024-10-24 19:29:01 字数 3489 浏览 6 评论 0 原文

我需要根据绑定中的文本块文本字符串值更改边框背景颜色。我计划使用触发器,但 Silverlight 不支持它。我正在寻找有关如何在 Silverlight 中实现它的任何建议。先感谢您!

XAML:

<data:DataGridTemplateColumn Header="Y Position" Width="100">
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Border Background="Red" Width="10" Height="18" VerticalAlignment="Center" Margin="0,0,10,0" />
                            <TextBlock Text="{Binding Y}" VerticalAlignment="Center" />
                        </StackPanel>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
            </data:DataGridTemplateColumn>

视图模型代码:

public class MainPage_ViewModel : INotifyPropertyChanged 
{
    public 
    public MainPage_ViewModel()
    {
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 }));
    }

    public ObservableCollection<Coordinate_DataViewModel> Coordinates     
    {         
        get { return coordinates; }         
        set          
        {             
            if (coordinates != value)             
            {                 
                coordinates = value;                 
                OnPropertyChanged("Coordinates");             
            }         
        }     
    }     
    private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();      
    public event PropertyChangedEventHandler PropertyChanged;      

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

    public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)     
    {         
        coordinates.Remove(dvmToDelete);     
    }

    public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete)
    {

    }
}

//Model
public class Coordinate_Model 
{     
    public double X;     
    public double Y; 
} 

//DataViewModel
public class Coordinate_DataViewModel 
{     
    public Coordinate_DataViewModel(Coordinate_Model model)     
    {         
        this.underlyingModel = model;     
    }     

    private Coordinate_Model underlyingModel;      
    public double X     
    {         
        get { return underlyingModel.X; }         
        set { underlyingModel.X = value; }     
    }      

    public double Y     
    {         
        get { return underlyingModel.Y; }         
        set { underlyingModel.Y = value; }     
    }      public string XYCoordinate     

    {         
        get { return "(" + X + "," + Y + ")"; }     
    } 
} 

I need to change border background color based of the textblock text string value from the binding. I planned to use triggers but it is not supported in Silverlight. I am looking for any advice on how it is achievable in Silverlight. Thank you in advance!

XAML:

<data:DataGridTemplateColumn Header="Y Position" Width="100">
                <data:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <StackPanel Orientation="Horizontal">
                            <Border Background="Red" Width="10" Height="18" VerticalAlignment="Center" Margin="0,0,10,0" />
                            <TextBlock Text="{Binding Y}" VerticalAlignment="Center" />
                        </StackPanel>
                    </DataTemplate>
                </data:DataGridTemplateColumn.CellTemplate>
            </data:DataGridTemplateColumn>

ViewModel code:

public class MainPage_ViewModel : INotifyPropertyChanged 
{
    public 
    public MainPage_ViewModel()
    {
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 1, Y = 2 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 2, Y = 4 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 3, Y = 6 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 4, Y = 8 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 5, Y = 10 }));         
        coordinates.Add(new Coordinate_DataViewModel(new Coordinate_Model() { X = 6, Y = 12 }));
    }

    public ObservableCollection<Coordinate_DataViewModel> Coordinates     
    {         
        get { return coordinates; }         
        set          
        {             
            if (coordinates != value)             
            {                 
                coordinates = value;                 
                OnPropertyChanged("Coordinates");             
            }         
        }     
    }     
    private ObservableCollection<Coordinate_DataViewModel> coordinates = new ObservableCollection<Coordinate_DataViewModel>();      
    public event PropertyChangedEventHandler PropertyChanged;      

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

    public void DeleteCoordinate(Coordinate_DataViewModel dvmToDelete)     
    {         
        coordinates.Remove(dvmToDelete);     
    }

    public void UpdateCoordinate(Coordinate_DataViewModel dvmToDelete)
    {

    }
}

//Model
public class Coordinate_Model 
{     
    public double X;     
    public double Y; 
} 

//DataViewModel
public class Coordinate_DataViewModel 
{     
    public Coordinate_DataViewModel(Coordinate_Model model)     
    {         
        this.underlyingModel = model;     
    }     

    private Coordinate_Model underlyingModel;      
    public double X     
    {         
        get { return underlyingModel.X; }         
        set { underlyingModel.X = value; }     
    }      

    public double Y     
    {         
        get { return underlyingModel.Y; }         
        set { underlyingModel.Y = value; }     
    }      public string XYCoordinate     

    {         
        get { return "(" + X + "," + Y + ")"; }     
    } 
} 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天暗了我发光 2024-10-31 19:29:01

我不太喜欢在视图模型中添加颜色。在我看来,最好使用转换器,如下所示:

public class CoordinateToColorConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        return new SolidColorBrush((int) value == 2 ? Colors.Red : Colors.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

然后您可以像这样定义绑定:

                    <Border Background="{Binding Y, Converter={StaticResource CoordinateToColorConverter}}" Width="10" Height="18" VerticalAlignment="Center" Margin="0,0,10,0" />

I'm not really a fan of putting colors inside your viewmodel. In my opinion it is best to use a converter then like so:

public class CoordinateToColorConverter : IValueConverter
{
    #region IValueConverter Members

    public object Convert(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        return new SolidColorBrush((int) value == 2 ? Colors.Red : Colors.Black);
    }

    public object ConvertBack(object value, Type targetType, object parameter,
        System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }

    #endregion
}

you can then define your binding like this:

                    <Border Background="{Binding Y, Converter={StaticResource CoordinateToColorConverter}}" Width="10" Height="18" VerticalAlignment="Center" Margin="0,0,10,0" />
憧憬巴黎街头的黎明 2024-10-31 19:29:01

我最近正在和别人讨论这个问题。我个人使用了 Luc Bos 提供的技术,但还有另一种技术:RYODTS 或 Roll Your Own DataTemplateSelector。

这可以通过相对较少的努力来完成。可以在 CodeProject 上找到示例。

I was having this discussion with someone else recently. I personally used the technique Luc Bos provided but there is another technique: RYODTS or Roll Your Own DataTemplateSelector.

This can be done with relative little effort. An example can be found on CodeProject.

过度放纵 2024-10-31 19:29:01

如果您使用 MVVM 开发模型,那么您将执行以下操作:

...并在 ViewModel 中创建一个属性,该属性提供你想要的颜色画笔。然后,有关该边框颜色的所有决策逻辑都可以驻留在您的数据旁边。

public double Y     
    {         
        get { return underlyingModel.Y; }         
        set { underlyingModel.Y = value; 
              // here, test underlyingModel.Y, and 
              // set backgroundColor private property, and 
              // raise the PropertyChanged event on "BackgroundColor" 
            }     
    }     

private Brush backgroundColor;
public Brush BackgroundColor { 
        get { return backgroundColor; }
        set { // whatever you want to do here, probably just  
              backgroundColor=value; 
              OnPropertyChanged("BackgroundColor")};
            }
        }

if you're using an MVVM development model, then you would do something like:

<Border Background="{Binding BackgroundColor}" ...

...and create a Property in your ViewModel which provides the color brush you want. Then, all the decision logic about that border color can reside next to your data.

public double Y     
    {         
        get { return underlyingModel.Y; }         
        set { underlyingModel.Y = value; 
              // here, test underlyingModel.Y, and 
              // set backgroundColor private property, and 
              // raise the PropertyChanged event on "BackgroundColor" 
            }     
    }     

private Brush backgroundColor;
public Brush BackgroundColor { 
        get { return backgroundColor; }
        set { // whatever you want to do here, probably just  
              backgroundColor=value; 
              OnPropertyChanged("BackgroundColor")};
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文