Thursday, July 31, 2014

StringFormat in WPF XAML

http://www.wpf-tutorial.com/data-binding/the-stringformat-property/

Dependency Property Value Precedence

When you get the value of a dependency property, you are potentially obtaining a value that was set on that property through any one of the other property-based inputs that participate in the WPF property system. Dependency property value precedence exists so that a variety of scenarios for how properties obtain their values can interact in a predictable way.
Consider the following example. The example includes a style that applies to all buttons and their Background properties, but then also specifies one button with a locally set Background value.
Note  
 
Note

The SDK documentation uses the terms "local value" or "locally set value" occasionally when discussing dependency properties. A locally set value is a property value that is set directly on an object instance in code, or as an attribute on an element in XAML.
In principle, for the first button, the property is set twice, but only one value applies: the value with the highest precedence. A locally set value has the highest precedence (except for a running animation, but no animation applies in this example) and thus the locally set value is used instead of the style setter value for the background on the first button. The second button has no local value (and no other value with higher precedence than a style setter) and thus the background in that button comes from the style setter.
 
 
<StackPanel>
  <StackPanel.Resources>
    <Style x:Key="{x:Type Button}" TargetType="{x:Type Button}">
     <Setter Property="Background" Value="Red"/>
    </Style>
  </StackPanel.Resources>
  <Button Background="Green">I am NOT red!</Button>
  <Button>I am styled red</Button>
</StackPanel>

Thursday, July 24, 2014

View Injection

http://www.wpftutorial.net/ReferenceArchitecture.html

Prism Event Aggregator

http://msdn.microsoft.com/en-us/library/ff921122.aspx

Prism Interactivity / Notification

http://msdn.microsoft.com/en-us/library/ff921081%28v=pandp.40%29.aspx

MVVM




http://msdn.microsoft.com/en-us/library/gg405484%28v=pandp.40%29.aspx

DependencyProperties


  • Change notification
    Dependency properties have a built-in change notification mechanism. By registering a callback in the property metadata you get notified, when the value of the property has been changed. This is also used by the databinding. 
  •  Value inheritance
    When you access a dependency property the value is resolved by using a value resolution strategy. If no local value is set, the dependency property navigates up the logical tree until it finds a value. When you set the FontSize on the root element it applies to all textblocks below except you override the value.
  • Reduced memory footprint
    It's a huge dissipation to store a field for each property when you think that over 90% of the properties of a UI control typically stay at its initial values. Dependency properties solve these problems by only store modified properties in the instance. The default values are stored once within the dependency property.
     





Here is a very good article for DependancyProperty.

http://wpftutorial.net/DependencyProperties.html

http://wpftutorial.net/DependencyProperties.html

DependencyProperty.Register

DependencyProperty.RegisterReadOnly

DependencyProperty.RegisterAttched

Wednesday, July 23, 2014

WPF ItemTemplate, DataTemplate exmaple

Very nice article about ItemTemplate and DataTemplete. Here is the link.

http://www.wpf-tutorial.com/list-controls/combobox-control/

WPF Visual Tree VS Logical Tree



Basically, logical tree elements are the ones we see on the UI and all other controls behind the scene that gets created in order to render them are includes in visual tree.

The Logical Tree

The logical tree describes the relations between elements of the user interface. The logical tree is responsible for:
  • Inherit DependencyProperty values
  • Resolving DynamicResources references
  • Looking up element names for bindings
  • Forwaring RoutedEvents

The Visual Tree

The visual tree contains all logical elements including all visual elements of the template of each element. The visual tree is responsible for:
  • Rendering visual elements
  • Propagate element opacity
  • Propagate Layout- and RenderTransforms
  • Propagate the IsEnabled property.
  • Do Hit-Testing
  • RelativeSource (FindAncestor)

Full article can be read from wpf helper site here 

http://wpftutorial.net/LogicalAndVisualTree.html


There are VisualTreeHelper and LogicalTreeHelper class as part of .NET frameworks.

We can find them here

http://msdn.microsoft.com/en-us/library/system.windows.logicaltreehelper%28v=vs.110%29.aspx

http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper%28v=vs.110%29.aspx





WPF Class Hierarchy


Tuesday, February 18, 2014

Monday, February 17, 2014

Update UI from ViewModel for INotifyCollectionChanged and INotifyPropertyChanged

Please see the example of loading trades data using ObservableCollection (which implements INotifyCollectionChanged) and reflecting updates in UI data grid automatically when trades are replaced with new trades here http://wpfgrid.blogspot.com/2014/02/simple-observablecollection-wpf-mvvm.html


Then please see how we have updated "Trade" class so that it implements INotifyPropertyChanged  interface here http://wpfgrid.blogspot.com/2014/02/inotifypropertychanged-implementation.html


Now if we replace the codes in the LoadTradeCommand's Execute method on fist example (top) with the following codes so that instead of every two seconds replacing with new trades data, it just updates the quantity and market price of the first time loaded trades every two seconds.




public void Execute(object parameter)
        {
            var loadedTrades = parameter as ObservableCollection<Trade>;
            if (loadedTrades != null)
            {
                TaskFactory taskfac = new TaskFactory();
                taskfac.StartNew(() =>
                    {
                        while (true)
                        {
                            var tradelist = Trades.LoadTradesFromDataSource();
                            App.Current.Dispatcher.Invoke(new Action(() =>
                            {
                                if (loadedTrades.Count == 0) // load only first time
                                {
                                    foreach (var trade in tradelist)
                                        loadedTrades.Add(trade);
                                }
                                else
                                {
                                    var mktpricetmp = DateTime.Now.Second; // just to get 
                                                                              some number

                                    var qtytmp = (DateTime.Now.Second + 5) * 1000; // just 
                                                                          to get some number



                                    foreach (var trade in loadedTrades)

                                    {

                                        trade.Qty = qtytmp.ToString();

                                        trade.MktPrice = mktpricetmp.ToString();

                                    }
                                }

                            }));

                            Thread.Sleep(2000);
                        }
                    });
            }


Now this will as well update data grid automatically as the trades data in the ObservableCollection are being updated.









Codes:

https://skydrive.live.com/#cid=BA4BC1E3D681B9B3&id=BA4BC1E3D681B9B3!112