dimanche 28 juin 2015

Custom button style alignment wpf

I tried modifying a button style in order to remove the Chrome System-like button. Everything works out fine there but the content of the button will not align properly at the center of the button.

Button Style:

<Style x:Key="NoChrome" TargetType="{x:Type Button}">
        <Setter Property="Background" Value="Transparent"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <ContentPresenter/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

Button declaration:

<Button Content="Button" Canvas.Left="141" Canvas.Top="324" Width="75" Style="{DynamicResource NoChrome}"/>

I am using entity framework for a homework and I have to remove some objects from the database. I would like to have a method to do so.

How can I call a method foo(DBSet arg) with another template class?

WPF TreeView Group By

I have class "Person" that look like this:

public class Person
{
        public string Name { get; set; }
        public int Age { get; set; }
        public string Country{ get; set; }
        public List<Person> Children { get; set; }
}

I want to create a TreeView in WPF that will present the Persons by Country.

For example:

  • New york
    • Bob
      • New York
        • Ethan
        • Owen
      • Canada
        • Benjamin
        • James
    • Kevin
      • Paris
        • Louis
    • William
  • Finland
    • Oliver
      • Hungary
        • Ben
        • Paul
      • Iceland
        • Arthur
          • Benjamin
      • England
        • Gabriel
        • Victor
    • Eino
      • Paris
        • Gabriel
        • Adam
    • Emma

My TreeView :

 <TreeView.Resources>
  <HierarchicalDataTemplate ItemsSource="{Binding People}" DataType="{x:Type Person}">
       <Label Content="{Binding Name}"></Label>
   </HierarchicalDataTemplate>

Is there someway to use "Group by" in tree view?

WPF dependency properties don't fire onChange on initialization

I use dependency properties and rely on PropertyValueChange handlers during initialization.

However, PropertyValueChange events don't fire upon initialization of default values of their properties, only on further changes. So I have to manually fire PropertyValueChange to force other components to pickup the first (default) property value.

Am I understand default dependency property default values incorrectly?

Is there any better way to initialize? I can't use static configuration in XAML for everything. I have some logic parts which rely on PropertyValueChange but can't by their nature be initialized from XAML.

thanks

Multiline text in WPF ListView

I have a WPF ListView that I need to populate with some text data. I would like this text to be on multiple lines in each ListView row.

Example:

Person 1 | Address
Age:     | Address2

Person 2 | Address
Age:     | Address2

Etc.

I would like to also format the text, and for my purposes this would all be in a one-column ListView.

If multiline is not possible, is there a better control for displaying a vertical list of binded data that allows for selection as well?

Show hidden WPF elements in Visual Studio designer

As opposed to this, does anybody has figured out a way to show all hidden elements while working in Visual Studio designer(or Blend)?

It's anti-productive to constantly change the default visibility property of elements to be able to see them while editing Xaml files.

WPF Build ContextMenu dynamically via MVVM

I have a TreeView where I display items bound via the TreeViews HierarchicalDataTemplate.ItemsSource. The context-menu of the TreeView changes based on which item is selected. The menu-items depend on the selected item. This means: the context-menu is built completely dynamic. For this purpose I wrote a MenuItemModel class, that serves as the business-object for a menu item. Like this:

public class MenuItemModel : ViewModelBase
{
  public string Header { get; set; }
  public string Icon { get; set; }
  public ObservableCollection<MenuItemModel> ChildItems { get; set; }
  public UiCommand Command { get; set; }
}

So far so good. But now I have two questions:

Question 1 How can I display a separator in the menu? I have another class SeparatorMenuItemModel that I planned to use for separators. But in that case my ContextMenu needs to contain a Separator and not a MenuItem. How can I do that?

Question 2 I tried to use a DataTemplate to customize how my MenuItems are displayed. But that does not change the menu itself, just the content-part. I'd have to use a ControlTemplate for that, but how can I make my Menu change ControlTemplate the way I could do with DataTemplate?