silverlight Grid ListBox行改变颜色

发布于 2024-12-10 14:01:17 字数 3576 浏览 0 评论 0原文

我有一个 silverlight 手机应用程序,我正在用它来学习。它使用网络服务来获取数据。我有一个网格,网格内有一个列表框。我希望能够根据条件将该列表框中的一些文本设置为某种颜色。

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="MainListBox" Margin="0,0,-12,0"  SelectionChanged="MainListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding Operation}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Text="{Binding DateTimeStart1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Text="{Binding DataID}" Visibility="Collapsed"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

因此,如果绑定到 Status 的文本块......如果该 status = true 我想更改行的颜色,甚至只是该文本框......无论什么都更容易。

我使用以下代码绑定数据

 Me.MainListBox.ItemsSource = e.Result

任何帮助都会很棒

~~~~~~~~~~~~~~~~~~~~~~~~~~~ 这是修改后的代码

  Private Class SomeData
    Inherits wsData.Data_Queries
    Public Property RowColor As SolidColorBrush
End Class

这是我创建该类的新实例并将其绑定到列表框

 Dim oSomeDataS As New List(Of SomeData)
    For Each x In e.Result
        Dim oSomeData As New SomeData
        With x
            oSomeData.DataID = .DataID
            oSomeData.DateTimeStart1 = .DateTimeStart1
            oSomeData.FinishFromStart = .FinishFromStart
            oSomeData.Operation = .Operation
            oSomeData.ShortDate = .ShortDate
            oSomeData.Status = .Status
            oSomeData.TblComputerNameID = .TblComputerNameID
            oSomeData.TblOperationID = .TblOperationID
            oSomeData.TblStatusID = .TblStatusID
            oSomeData.TblSiteID = .TblSiteID

            If .Status = "False" Then
                oSomeData.RowColor = New SolidColorBrush(Colors.Red)
            Else
                oSomeData.RowColor = New SolidColorBrush(Colors.Green)
            End If
            oSomeDataS.Add(oSomeData)
        End With
    Next
    Me.MainListBox.ItemsSource = oSomeDataS.OrderBy(Function(o) o.Operation)

和 XAML 代码的地方

 <ListBox x:Name="MainListBox" Margin="0,0,-12,0"  SelectionChanged="MainListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding Operation}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="{Binding RowColor}"/>
                        <TextBlock Text="{Binding DateTimeStart1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Text="{Binding DataID}" Visibility="Collapsed"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

I've got a silverlight phone app that i'm using to learn. It's using web services to get the data. I've got a Grid with a listbox inside of the grid. I'd like to be able to have some of the text inside that listbox be a certain color based on conditions.

 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <ListBox x:Name="MainListBox" Margin="0,0,-12,0"  SelectionChanged="MainListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding Operation}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Text="{Binding DateTimeStart1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Text="{Binding DataID}" Visibility="Collapsed"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>

So if the textblock which is bound to Status.. if that status = true i want to change the color of the row or even just that textbox.... whatever is easier.

i'm binding the data using the following code

 Me.MainListBox.ItemsSource = e.Result

Any help would be great

~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the modified code

  Private Class SomeData
    Inherits wsData.Data_Queries
    Public Property RowColor As SolidColorBrush
End Class

And here is where i'm creating a new instance of the class and binding it to the listbox

 Dim oSomeDataS As New List(Of SomeData)
    For Each x In e.Result
        Dim oSomeData As New SomeData
        With x
            oSomeData.DataID = .DataID
            oSomeData.DateTimeStart1 = .DateTimeStart1
            oSomeData.FinishFromStart = .FinishFromStart
            oSomeData.Operation = .Operation
            oSomeData.ShortDate = .ShortDate
            oSomeData.Status = .Status
            oSomeData.TblComputerNameID = .TblComputerNameID
            oSomeData.TblOperationID = .TblOperationID
            oSomeData.TblStatusID = .TblStatusID
            oSomeData.TblSiteID = .TblSiteID

            If .Status = "False" Then
                oSomeData.RowColor = New SolidColorBrush(Colors.Red)
            Else
                oSomeData.RowColor = New SolidColorBrush(Colors.Green)
            End If
            oSomeDataS.Add(oSomeData)
        End With
    Next
    Me.MainListBox.ItemsSource = oSomeDataS.OrderBy(Function(o) o.Operation)

And the XAML code

 <ListBox x:Name="MainListBox" Margin="0,0,-12,0"  SelectionChanged="MainListBox_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Margin="0,0,0,17" Width="432">
                        <TextBlock Text="{Binding Operation}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <TextBlock Text="{Binding Status}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}" Foreground="{Binding RowColor}"/>
                        <TextBlock Text="{Binding DateTimeStart1}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                        <TextBlock Text="{Binding DataID}" Visibility="Collapsed"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

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

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

发布评论

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

评论(1

旧瑾黎汐 2024-12-17 14:01:17

有很多不同的方法可以做到这一点。最容易理解的是使用 IValueConverter 将行的背景颜色绑定到基于状态的颜色。可以在此处找到一个很好的示例(示例是WPF,但同样的原理也适用于Silverlight)。

另一种方法是将数据对象(从 e.Result 获取)包装在 ViewModel 对象中。 ViewModel 对象将公开与您的数据相同的所有属性,但还会添加一个属性(可能是 RowColor),其中 getter 根据 Status 的值返回 Brush 或 Color >。您必须循环遍历 e.Results 并为每个结果创建一个新的 ViewModel,将 ViewModel 添加到列表中,然后将 ItemsSource 设置为该新的 ViewModel 列表。

There are lots of different ways to do this. The easiest to understand is to use an IValueConverter to bind the background color of the row to a color based on the status. A good example of this can be found here (the example is WPF, but the same principle applies in Silverlight).

Another way to do this is to wrap your data objects (that you get from e.Result) in a ViewModel object. The ViewModel object would expose all the same properties as your data, but would also add one property (perhaps RowColor) where the getter returned a Brush or Color based on the value of Status. You'd have to loop through your e.Results and create a new ViewModel for each one, add the ViewModels to a list, then set the ItemsSource to that new list of ViewModels.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文