How to do the binding in this case?
As the code below shows, I have a Canvas
with a TextBlock
inside. Next to it, two textBlocks, their text properties binded to LineOne and LineTwo, ObservableCollection
variables. What I want is to create a binding which gives the date and change the color of the Canvas
accordingly.
Theoretically, I can bind the date change for the Text
property of the DateTextBlock
. But I am not sure how to do the color change of the Canvas
.
<DataTemplate>
<StackPanel Orientation="Horizontal" Margin="0,0,0,17">
<!--Replace rectangle with image-->
<Canvas Width="100" Height="100" Background="YellowGreen">
<StackPanel>
<TextBlock Name="DateTextBlock" Text="16 May" HorizontalAlignment="Center" VerticalAlignment="Center" />
</StackPanel>
</Canvas>
<StackPanel Width="311">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="开发者_开发百科{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</StackPanel>
</DataTemplate>
Kindly advise me. Thanks,
Create a colour property on the view model, and a property changed notifier. Bind the view attribute to the Colour property. In the viewmodel, when the text changes (or whatever it is you want to trigger the colour change), update the Colour property with the appropriate colour making sure to use the public property so that the change event fires.
The Canvas
Background
property is of type Brush
. Therefore, you are either going to have to bind a property of type Brush
to Canvas.Background
, or you can use a ValueConverter
to convert a moel property to a Brush
. See the examples on this page:
http://compiledexperience.com/blog/posts/useful-calue-converters
精彩评论