1
I’m creating a dynamic menu using Tiles
(Mahapps). I have two types of Tiles
that I can use: Largetilestyle and Smalltilestyle. For this, I have a function that allows me to pass the name of Style
via parameter and set the property Style
, when the Tile
was created.
Creation of Tiles
:
this.Tile.Add(
new TileItem()
{
Icon = new PackIconFontAwesome()
{
Kind = PackIconFontAwesomeKind.Angellist
},
Text = "Usuários",
Style = "LargeTileStyle",
Background = "Green",
NavigationDestination = new Uri("Views/UsuarioList.xaml", UriKind.RelativeOrAbsolute)
}
);
this.Tile.Add(
new TileItem()
{
Icon = new PackIconFontAwesome()
{
Kind = PackIconFontAwesomeKind.Apple
},
Text = "Clientes",
Style = "SmallTileStyle",
Background = "Blue",
NavigationDestination = new Uri("Views/ClienteList.xaml", UriKind.RelativeOrAbsolute)
}
);
Class TileItem
:
internal class TileItem : BindableBase
{
private object _icon;
private string _text;
private string _style;
private string _background;
private DelegateCommand _command;
private Uri _navigationDestination;
public object Icon
{
get { return this._icon; }
set { this.SetProperty(ref this._icon, value); }
}
public string Text
{
get { return this._text; }
set { this.SetProperty(ref this._text, value); }
}
public string Style
{
get { return this._style; }
set { this.SetProperty(ref this._style, value); }
}
public string Background
{
get { return this._background; }
set { this.SetProperty(ref this._background, value); }
}
public ICommand Command
{
get { return this._command; }
set { this.SetProperty(ref this._command, (DelegateCommand)value); }
}
public Uri NavigationDestination
{
get { return this._navigationDestination; }
set { this.SetProperty(ref this._navigationDestination, value); }
}
public bool IsNavigation => this._navigationDestination != null;
}
The problem is that in the block below, when I do one Binding
to set the property Style
, does not work because he can not set the names I pass by parameter. Someone knows how to help me?
Style:
<ResourceDictionary>
<Style x:Key="LargeTileStyle" TargetType="Controls:Tile">
<Setter Property="Width" Value="300" />
<Setter Property="Height" Value="125" />
<Setter Property="TitleFontSize" Value="16" />
</Style>
<Style x:Key="SmallTileStyle" TargetType="Controls:Tile">
<Setter Property="Width" Value="147" />
<Setter Property="Height" Value="125" />
<Setter Property="TitleFontSize" Value="16" />
</Style>
</ResourceDictionary>
Layout of my Window:
<Grid>
<ListBox ItemsSource="{Binding Tile}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<controls:Tile
Title="{Binding Text}"
controls:ControlsHelper.MouseOverBorderBrush="{DynamicResource BlackBrush}"
Command="{Binding DataContext.TileClickCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"
CommandParameter="{Binding}"
HorizontalTitleAlignment="Left"
Background="{Binding Background}"
Style="{Binding Style}"
TiltFactor="2">
<Image Width="60"
Height="60"/>
<!--<Source="{Binding OmsConnectionTypeId, Converter={StaticResource ConnectionTypeToIconConverter}}"/>-->
</controls:Tile>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Thanks for helping Perozzo, but I didn’t understand the part where you say: "To use this method, your class must belong to Frameworkelement, such as the code Behind (.Cs) of a Window." I don’t know how this works...
– Master JR
I tried, put in the Page Load method, but it didn’t work...
– Master JR
That would have to stay within some function?
– Master JR
What didn’t work? Did it go wrong or just didn’t work? If it didn’t work it’s because of your Binding. As I said in the other answer, the Binding of the Style property can change depending on how you designed your app for Binding. I tested this solution before publishing the answer and it worked perfectly.
– perozzo
It was underlined in red the "Findresource"... I didn’t understand why I set my Style Operty with smallTileStyle: nameSeuObject.Style = smallTileStyle; and if I need to set with Largetilestyle?
– Master JR
If it was underlined in red it’s because you’re using it in the wrong place. In what class did you put it? If you have placed yourself in the Tileitem class it will not work because as mentioned your class must be a Frameworkelement. About smallTileStyle is just one example, if you want to use largeTileStyle, you assign it instead of the other. When creating your Tileitem object, you place either Smalltilestyle or Largetilestyle in the Style property.
– perozzo
In what class do you create the Tiles? Up there you mentioned the "Creation of the Tiles". In what class do you do this?
– perozzo
Let’s go continue this discussion in chat.
– perozzo
Tileitem class
– Master JR
Get in the chat room so we can get this over with.
– perozzo