Skip to content

Instantly share code, notes, and snippets.

@nngogol
Created December 15, 2017 09:31
Show Gist options
  • Save nngogol/bf672e9f423adbb481261fbece7a7533 to your computer and use it in GitHub Desktop.
Save nngogol/bf672e9f423adbb481261fbece7a7533 to your computer and use it in GitHub Desktop.
crud for catel
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>Define a curd structure</Title>
<Shortcut>crudVM</Shortcut>
<Description>Final Code snippet for a crud structure with C# + XAML code</Description>
<Author>NNGogol</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>entity_name</ID>
<ToolTip>Name of your model</ToolTip>
<Default>entity_name</Default>
</Literal>
<Literal>
<ID>type</ID>
<ToolTip>Type of your model</ToolTip>
<Default>type</Default>
</Literal>
</Declarations>
<Code Language="csharp">
<![CDATA[
// Constructor data
addNew$entity_name$ = new Command(OnaddNew$entity_name$Execute);
saveState = new Command(OnsaveStateExecute);
del_selected_$entity_name$ = new Command(Ondel_selected_$entity_name$Execute);
// potensial$entity_name$ - item do add
// selItem_$entity_name$ - currently selected item in listview
// list_$entity_name$ - collection of items in listview
public static readonly PropertyData potensial$entity_name$Property = RegisterProperty("potensial$entity_name$", typeof($type$), null);
public static readonly PropertyData selItem_$entity_name$Property = RegisterProperty("selItem_$entity_name$", typeof($type$), null);
public static readonly PropertyData list_$entity_name$Property = RegisterProperty("list_$entity_name$", typeof(ObservableCollection<$type$>), new ObservableCollection<$type$>());
public $type$ potensial$entity_name$ {get {return GetValue<$type$>(potensial$entity_name$Property); } set {SetValue(potensial$entity_name$Property, value); } }
public $type$ selItem_$entity_name$ {get {return GetValue<$type$>(selItem_$entity_name$Property); } set {SetValue(selItem_$entity_name$Property, value); } }
public ObservableCollection<$type$> list_$entity_name$ {get {return GetValue<ObservableCollection<$type$>>(list_$entity_name$Property); } set {SetValue(list_$entity_name$Property, value); } }
public Command addNew$entity_name$ {get; private set; }
public Command saveState {get; private set; }
public Command del_selected_$entity_name$ {get; private set; }
// ADD (new entry)
private void OnaddNew$entity_name$Execute()
{
list_$entity_name$.Add(potensial$entity_name$);
potensial$entity_name$ = null;
}
// SAVE (state)
private void OnsaveStateExecute()
{
// save...
//File.WriteAllLines(@"path/to/somewhere", list_$entity_name$.ToArray());
}
// DEL (selected)
private void Ondel_selected_$entity_name$Execute()
{
list_$entity_name$.Remove(selItem_$entity_name$);
}
//=======================
<Grid>
<Grid.RowDefinitions> <RowDefinition Height="4*" /> <RowDefinition Height="1*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="2*" /> <ColumnDefinition Width="1*" /> <ColumnDefinition Width="1*" /> </Grid.ColumnDefinitions>
<Grid.InputBindings> <KeyBinding Key="Delete" Command="{Binding del_selected_$entity_name$}" /> </Grid.InputBindings>
<StackPanel Grid.Row="0" Grid.RowSpan="1" Grid.ColumnSpan="4">
<Button Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" Height="30" Margin="5,0" VerticalAlignment="Bottom"
Command="{Binding saveState}" Content="Save changed" />
<ListView MinHeight="200" ItemsSource="{Binding list_$entity_name$}" SelectedItem="{Binding selItem_$entity_name$}" >
<ListView.ContextMenu> <ContextMenu> <MenuItem Command="{Binding del_selected_$entity_name$}" Header="delete" /> </ContextMenu> </ListView.ContextMenu>
</ListView>
</StackPanel>
<Button Grid.Row="1" Grid.RowSpan="2" Grid.Column="0" Height="30" Margin="5,0,5,5" VerticalAlignment="Bottom" Command="{Binding addNew$entity_name$}"
IsEnabled="{Binding ElementName=potensial$entity_name$Var, Path=Text.Length, Mode=OneWay}" Content="Add new item" />
<TextBox Grid.Row="1" x:Name="potensial$entity_name$Var" Grid.RowSpan="2" Grid.Column="1" MinWidth="200" Margin="0,2" Text="{Binding potensial$entity_name$}" > </TextBox>
<Button Grid.Row="1" Grid.RowSpan="2" Grid.Column="3" Height="30" MinWidth="100" Margin="5" Command="{Binding del_selected_$entity_name$}" Content="Delete selected item" /> </Grid>
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment