Reputation: 2214
I'm still having a lot of trouble to properly understand binding in XAML on windows phone 8.1 and papers on MSDN don't really seem to help me.
I have two structures. One that fills the data inside the ListBox
which already works great and one, that should set the color, which I don't really understand on how to do that.
My code so far looks like this:
<ListBox x:Name="ListBox1" Margin="5" Width="Auto" Height="300" HorizontalAlignment="Left" ItemsSource="{Binding}" >
<ListBox.ItemTemplate>
<DataTemplate>
<Border x:Name="BorderTap" BorderThickness="2" CornerRadius="15" Margin="0" Height="80" Width="Auto" VerticalAlignment="Top">
<Border.BorderBrush>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{Binding borderStart}" Offset="0"/>
<GradientStop Color="{Binding borderStart}" Offset="0.7"/>
<GradientStop Color="{Binding borderEnd}" Offset="1"/>
</LinearGradientBrush>
</Border.BorderBrush>
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{Binding boxStart}" Offset="0"/>
<GradientStop Color="{Binding boxStart}" Offset="0.7"/>
<GradientStop Color="{Binding boxEnd}" Offset="1"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Image HorizontalAlignment="Left" Margin="2,0,0,0" Height="120" Width="120" VerticalAlignment="Top" Source="Assets/due.png" Stretch="Fill" Grid.Row="1" Opacity="0.4" />
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Top" Margin="0,0,0,0" Grid.RowSpan="2">
<StackPanel Width="30" HorizontalAlignment="Left" VerticalAlignment="Top" />
<StackPanel HorizontalAlignment="Left" Height="Auto" Margin="0,0,0,0" VerticalAlignment="Center" Width="Auto">
<Image HorizontalAlignment="Left" Height="51" Margin="0" Width="51" Source="Assets/fish.png" Stretch="Fill" RenderTransformOrigin="2.307,0.881" VerticalAlignment="Center"/>
</StackPanel>
<StackPanel Width="10" HorizontalAlignment="Left" VerticalAlignment="Top" />
<StackPanel HorizontalAlignment="Left" Height="80" Width="Auto" Margin="0" Orientation="Vertical">
<TextBlock HorizontalAlignment="Left" Height="Auto" Margin="0,20,0,0" TextWrapping="Wrap" Text="{Binding Name}" Width="Auto" VerticalAlignment="Top" FontSize="19" Foreground="Black" FontWeight="Bold"/>
<TextBlock HorizontalAlignment="Left" Height="Auto" Margin="0" TextWrapping="Wrap" Text="{Binding URL}" Width="Auto" VerticalAlignment="Top" Foreground="#FF0097FF"/>
</StackPanel>
</StackPanel>
</Grid>
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the structures
like this:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
public class ColorContainer
{
public string boxStart { get; set; }
public string boxEnd { get; set; }
public string borderStart { get; set; }
public string borderEnd { get; set; }
public string firstEntry { get; set; }
public string secondEntry { get; set; }
}
How can I set the colors? Or where do I define them and update them? And second question: After that, how can I select a value and add then another information from a third structure?
Thank you very much in advande
Upvotes: 0
Views: 358
Reputation: 12993
The type of GradientStop.Color is Color
, so you need to define ColorContainer.boxStart as Color, or you need a Converter
to convert string into Color, but to keep it simple, I will not use Converter since that adds more complexity to the code.
public class ColorContainer
{
public Color boxStart { get; set; }
public Color boxEnd { get; set; }
}
And I add a ColorContainer property to ComboboxItem
.
public class ComboboxItem
{
public string Name { get; set; }
public object URL { get; set; }
public ColorContainer CContainer { get; set; }
public override string ToString()
{
return Name;
}
}
And I define a MyDataContext
class which contain the list of ComboboxItem
s for binding
public class MyDataContext
{
public MyDataContext()
{
ColorContainer _cContainer = new ColorContainer(); ;
_cContainer.boxStart = Colors.Orange;
_cContainer.boxEnd = Colors.Green;
//note that all items use this _cContainer instance
_items = new ObservableCollection<ComboboxItem>();
_items.Add(new ComboboxItem() { Name = "name1", URL = "url1", CContainer = _cContainer });
_items.Add(new ComboboxItem() { Name = "name2", URL = "url2", CContainer = _cContainer });
}
ObservableCollection<ComboboxItem> _items;
public ObservableCollection<ComboboxItem> Items
{
get { return _items; }
}
}
Finally, the binding code:
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="{Binding CContainer.boxStart}" Offset="0"/>
<GradientStop Color="{Binding CContainer.boxStart}" Offset="0.7"/>
<GradientStop Color="{Binding CContainer.boxend}" Offset="1.0"/>
</LinearGradientBrush>
</Border.Background>
Don't forget the set the DataContext of the page, as you already did.
// Constructor
public MainPage()
{
InitializeComponent();
this.DataContext = new MyDataContext();
// Sample code to localize the ApplicationBar
//BuildLocalizedApplicationBar();
}
Update
to fix the problem in your comment regarding the width of the listboxitem, add this
<ListBox x:Name="ListBox1" Margin="5" Height="300" HorizontalAlignment="Stretch" ItemsSource="{Binding Items}" >
<!--add this-->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<!--end-->
<ListBox.ItemTemplate>
<DataTemplate>
....
Upvotes: 1