如何从Windows Phone 8中的数据绑定设置样式staticresource

时间:2014-02-21 05:30:08

标签: windows-phone-8

如何从数据绑定中动态设置样式staticresource

我使用下面的代码来改变每个网格的样式

 <Grid HorizontalAlignment="Left" Width="453" Background="{StaticResource {Binding mystatus}}"  >

整个是

<phone:LongListSelector x:Name="MainLongListSelector" Margin="5,5" ItemsSource="{Binding TaskList}" SelectionChanged="MainLongListSelector_SelectionChanged" Foreground="Black" Height="654">
                <phone:LongListSelector.ItemTemplate>
                    <DataTemplate>
                        <Border Style="{StaticResource bordercss}" >
                            <Grid HorizontalAlignment="Left" Width="453" UseOptimizedManipulationRouting="True" Background="{StaticResource {Binding mystatus}}"  >
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition Width="5*"/>
                                    <ColumnDefinition Width="6*" />
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                    <RowDefinition></RowDefinition>
                                </Grid.RowDefinitions>
                                <TextBlock Grid.Row="0" Grid.Column="0" Margin="5" Text="{Binding no}" />
                                <StackPanel Orientation="Vertical" Grid.Row="0" Grid.Column="1" >
                                   <TextBlock   Text="{Binding title}" Style="{StaticResource textbold}" Margin="5,0" />
                                    <TextBlock   Text="{Binding csoId}" Style="{StaticResource btbx}" Margin="5,0,5,5" HorizontalAlignment="Left" />

                                </StackPanel>

                                <Image  HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"  Source="{Binding imageurl}" Margin="5" Height="25" />

                                <Image HorizontalAlignment="Left"   Grid.Row="1" Grid.Column="0"  Source="/Assets/client_icon.png" Margin="5,5,0,5" Width="23"/>
                                <TextBlock  Grid.Row="1" Grid.Column="1" Text="{Binding clientname}" Style="{StaticResource textsimple}"  />

                                <Image  HorizontalAlignment="Left" Grid.Row="2" Grid.Column="0"  Source="/Assets/contact_icon.png" Margin="5,5,0,5" Width="17"/>
                                <TextBlock  Grid.Row="2" Grid.Column="1" Text="{Binding customercontact}" Style="{StaticResource textsimple}"   />

                                <Image HorizontalAlignment="Left" Grid.Row="3" Grid.Column="0"  Source="/Assets/address_icon.png" Margin="5,5,0,5" Width="20"/>
                                <TextBlock  Grid.Row="3" Grid.Column="1" Text="{Binding address}" Style="{StaticResource textsimple}"   />

                                <StackPanel Grid.Row="4" Grid.ColumnSpan="2" Grid.Column="0" Style="{StaticResource mainstp}">
                                    <TextBlock Text="{Binding mydate}" Style="{StaticResource btbx}" />
                                    <TextBlock Text="{Binding mytime}" Style="{StaticResource btbx}" />
                                </StackPanel>

                                <StackPanel Grid.Row="4" Grid.ColumnSpan="2" Grid.Column="2" Style="{StaticResource mainstp}">
                                    <TextBlock Text="status" Style="{StaticResource btbx}" />
                                    <TextBlock Text="{Binding mystatus}" Style="{StaticResource btbx}" Foreground="Blue"/>
                                </StackPanel>
                                <!--<Button Grid.Row="4" Grid.ColumnSpan="2" VerticalAlignment="Center"  HorizontalAlignment="left" Grid.Column="0" Content="{Binding expdate}" Margin="-12,0,-12,-12" Background="#FFD6D6D6" Foreground="Black" Height="80"  />

                                <Button Grid.Row="4" Grid.Column="2" Content="Status" Margin="-10,0,-12,-12" Background="#FFD6D6D6" Foreground="Black" Padding="0,0,5,0" Height="80"  />-->

                            </Grid>

                        </Border>

                    </DataTemplate>
                </phone:LongListSelector.ItemTemplate>
            </phone:LongListSelector>

1 个答案:

答案 0 :(得分:-1)

最后我得到了解决方案把这个道具放在Model

  private string _mystatus;
    public string mystatus
    {
        get
        {
            return _mystatus;
        }
        set
        {
            if (value != _mystatus)
            {
                _mystatus = value;
                NotifyPropertyChanged("mystatus");
                NotifyPropertyChanged("gridbgc");
                NotifyPropertyChanged("buttonfgc");
            }
        }
    }
    public string gridbgc
    {
        get
        {
            switch (mystatus)
            {
                case "Checked In":
                    return "#C8D7FF";
                case "Pending":
                    return "White";
                default:
                    return "White";
            }
        }
    }       
     public string buttonfgc
    {
        get
        {
            switch (mystatus)
            {
                case "Completed":
                    return "#6FCE2A";  
                case "Pending":
                    return "C03F40";
                default:
                    return "#6691EB";
            }
        }
    }

你可以通过“Binding Dynamicall Forecolor

获得这个
  

buttonfgc

   <TextBlock Text="{Binding mystatus}" Style="{StaticResource btbx}" Foreground="{Binding buttonfgc}"/>