2013-11-28

Custom DependencyProperty

CODE :

public class baseComboBoxItem : ComboBoxItem
    {
        public static DependencyProperty IsAllowHighlightProperty = DependencyProperty.Register("IsAllowHighlight", typeof(bool), typeof(baseComboBoxItem), new PropertyMetadata(true));  //the "true" is for custom default value, because the type is "bool"
        public baseComboBoxItem()
        {
         
        }

        public bool IsAllowHighlight
        {
            get
            {
                return (bool)base.GetValue(IsAllowHighlightProperty);
            }
            set
            {
                base.SetValue(IsAllowHighlightProperty, value);
            }
        }
    }

So, we can use IsAllowHighlight in XAML

<BC:baseComboBox x:Class="MakeMoney.BaseControls.comboColorPicker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:BC="clr-namespace:MakeMoney.BaseControls"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300"
             Width="100" Height="26" SelectionChanged="baseComboBox_SelectionChanged">  
    <BC:baseComboBoxItem Background="#FF000000" IsAllowHighlight="False"><TextBlock Text="" /></BC:baseComboBoxItem>
    <BC:baseComboBoxItem Background="#FFCCCCCC"><TextBlock Text="" /></BC:baseComboBoxItem>
    <BC:baseComboBoxItem Background="#FFDDDDDD"><TextBlock Text="" /></BC:baseComboBoxItem>
</BC:baseComboBox>

And also, for template triggers

<ControlTemplate.Triggers>
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="IsHighlighted" Value="True"/>
                                <Condition Property="IsAllowHighlight" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="Background" TargetName="Bd" Value="{StaticResource baseHighlightedBackground}"/>
                        </MultiTrigger>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>

2013-11-25

Use the image in style setting

Environment :

In resource dictionary, load image in style

When new instance apply the style, previous control who applied this style will lost the image.



Solution :

Add x:Shared="False" for style


EX:
<Style TargetType="{x:Type BC:captionButtonMin}" BasedOn="{StaticResource styleCaptionButton}" x:Shared="False">
        <Setter Property="BorderThickness" Value="1,1,1,1"/>
        <Setter Property="CornerRadius" Value="0,0,0,4"/>
        <Setter Property="Content">
            <Setter.Value>
                <Image Source="/Resources/Images/captionButtonMIN.jpg" Width="12" Margin="0,4,0,0"/>
            </Setter.Value>
        </Setter>      
    </Style>



Weird :

x:Shared does not show up from Intellisense??

2013-11-12

Get type from string and new an instance

menuItem.Tag = "namespace.class, dllname";
Type windowType = Type.GetType((string)(menuItem.Tag));
Control window = (Control)Activator.CreateInstance(windowType);
window.Show();

Store type from generic methods

public class baseMenuItem : MenuItem
    {      

        Type handleWindowType;

        public baseMenuItem()
        {
            this.Click += baseMenuItem_Click;
        }

        void baseMenuItem_Click(object sender, RoutedEventArgs e)
        {
            if (null != handleWindowType)
            {
                baseWindow win = (baseWindow)Activator.CreateInstance(handleWindowType);
                win.Show();
            }
         
            e.Handled = true;   //To prevent routed event triggered
        }

        public void AssignHandleWindow<T>() where T : baseWindow
        {
            handleWindowType = typeof(T);          
        }
    }