Title

Tuesday, 20 January 2015

How can I access UI element in the winrt IValueConverter?


I am developing on the winrt. And I want to access parent UI elemnet in ValueConverter. Eventually I want to change the parent's VisualState by checking the each item's inputted datacontext. This value converter is some kind of fake stuff that works as same input and same output. It works on the entrypoint of inputting datacontex of each item.

My working codes follows...

public class MvvmTestPageLbObjVsConverter : IValueConverter  {   public object Convert(object value, Type targetType, object parameter, string language)   {   MockupMember member = (MockupMember)value;   Control control = (Control)parameter;     if (member.Name[0] == 'κΉ€')   {   VisualStateManager.GoToState(control, "VsFamilyNameKim", false);   }   else   {   VisualStateManager.GoToState(control, "VsFamilyNameDef", false);   }     return value;   }     public object ConvertBack(object value, Type targetType, object parameter, string language)   {   throw new NotImplementedException();   }  }

And My xaml markup codes follows...

<Page.Resources>   <local:MvvmTestPageLbObjVsConverter x:Name="MvvmTestPageLbObjVsConverter" />  </Page.Resources>  <Page.DataContext>   <local:MvvmTestPageVm />  </Page.DataContext>  <Grid>   <ListView x:Name="LvObj" />   <ListBox x:Name="LbObj" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Top"   ItemsSource="{Binding Members}">   <ListBox.ItemContainerStyle>   <Style TargetType="ListBoxItem">   <Setter Property="Template">   <Setter.Value>   <ControlTemplate TargetType="ListBoxItem">   <Grid x:Name="GdBg" Background="#FF888888">   <Grid.Resources>   <local:MvvmTestPageLbObjVsConverter x:Key="MvvmTestPageLbObjVsConverter" />   </Grid.Resources>   <Grid.DataContext>   <Binding Converter="{StaticResource MvvmTestPageLbObjVsConverter}"   ConverterParameter="{RelativeSource Mode=Self}"></Binding>   </Grid.DataContext>   <VisualStateManager.VisualStateGroups>   <VisualStateGroup x:Name="VsgFamilyName">   <VisualState x:Name="VsFamilyNameDef" />   <VisualState x:Name="VsFamilyNameKim">   <Storyboard>   <ColorAnimation Duration="0" To="Magenta"   Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)"   Storyboard.TargetName="GdBg" d:IsOptimized="True" />   </Storyboard>   </VisualState>   </VisualStateGroup>   </VisualStateManager.VisualStateGroups>   <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Id}"   VerticalAlignment="Top" FontSize="20" />   <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Name}"   VerticalAlignment="Top" Margin="100,0,0,0" FontSize="20" Foreground="Lime" />   <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Address}"   VerticalAlignment="Top" Margin="200,0,0,0" FontSize="20" Foreground="Yellow" />   </Grid>   </ControlTemplate>   </Setter.Value>   </Setter>   </Style>   </ListBox.ItemContainerStyle>   </ListBox>  </Grid>
Answer

No comments:

Post a Comment