开发者

How to bind Listbox to two properties?

In Silverlight, I have a Grid with DataContext set to class ViewModel.

The ViewModel contains list of items (each of them containing int ID and string Text) and an integer "ID", which identifies the currently active item (not selected item).

I would like to construct xaml with ListBox where activated item has another color. H开发者_运维技巧ow can I do it?

Specifically, in xaml, I have:

<Grid DataContext="ModelView">
    <ListBox ItemsSource="Questions">
        <ListBox.ItemTemplate>           
            <TextBlock Text="{Binding ID}" />
            <TextBlock Text="{Binding Text}" />           
        </ListBox.ItemTemplate>
    </ListBox>
    <TextBlock Text="{Binding ID}" />
</Grid>`

How can set I the color of one (and only one) item in the listbox, based on property ID in ModelView?

One more problem - when I change active item - how can I refresh the ListBox?


I've in xaml: <Grid DataContext="ModelView"> <ListBox ItemsSource="Questions"> <ListBox.ItemTemplate>
<TextBlock Text="{Binding ID}" /> <TextBlock Text="{Binding Text}" />
</ListBox.ItemTemplate> </ListBox> <TextBlock Text="{Binding ID}" /> </Grid>

How set I color of one (and only one) item in th listbox based on property ID in ModelView?


Your ItemTemplate does not seem to be valid btw. !

I would try to use Triggers to template your highlighted Listbox Item

In a Grid i did this one for example

 <DataTemplate>
     <Image x:Name="MyImage" Source="Images/corrected.png" Width="64" Height="64" />
     <DataTemplate.Triggers>
        <DataTrigger Binding="{Binding YourConditionValue}" Value="0">
            <Setter TargetName="MyImage" Property="Source" Value="Images/notCorrected.png" />
         </DataTrigger>
      </DataTemplate.Triggers>
  </DataTemplate>

That might be a solution but you need to check it yourself

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜