This project is read-only.
 

Prometheus.Binding


Use this link to learn how to install Prometheus.Binding from NuGet console
Detailed information about Prometheus.Binding API could be found at Prometheus.Binding API

About

Binding is the mechanism to synchronize value of control property to any properties in data model. This is declarative way. It means you don't need to write code to get the value and to set the value, you just have to declare what properties must be synchronized.
Look at this code:
<TextBlock Text="{pro:proValidationBinding Path=Price}"/>
There is a property named "Price" at DataContext's object and it must be bound to property "Text", which will be displayed within TextBlock control.

Feautures

Prometheus binding system is the alternative and addition for WPF binding. It works the same way but provide some additional feautures:
  • Great CastOrConvert mechanism. It tries to cast, it tries to use IConvertible, it almost tries to convert array of bytes into ImageSource. Just look at dirty Cast or Convert Source .
  • INotifyPropertyChanged works not only for datacontext's object, but for subproperties as well. In case of path Order.Customer.Address.City.Name system looks for INotifyPropertyChanged interface implementation in each object: Order,Customer,Address and City.
  • Ability to split bindings on page in to groups, update group of bindings manually or when the current binding updates or update current binding when any binding in the group updates and much more
  • Ability to bind not only to DependencyProperty but to any simple property as well
  • You can connect more then one binding to one control property
  • Powerfull validation possibilities: by setter exception, by validation rules or by implementing simple interface IDataError
  • proNullContextBinding - you can bind and validate user input even there is no any data context, you can use validation rules before or after converting
  • classes are public any ready for overriding, just derive from anything and create your own bindings, converters, validation rules or keep existent: customize and adapt it to work with your own controls
  • Prometheus.Binding uses datacontext like built-in binding system does. Mix it together and get most greatest feautures from each of technologies

No need in MVVM

You don't want to create additional model class only for one property or only for one/two lines of code?
No problem - you can create rules declaratively directly in XAML. Inject your code with proDelegateBinding and proDelegateValidationRule. It will allow you to handle data processing in code-behind.
What did you say? No MVVM? What? Is this joke? MVVM is ...
Does not matter - use MVVM as you like or use any pattern you want. Prometheus.Binding will be fine on any way where XAML is being used.

Try it

Try to move mouse over red triangle and then type something in to TextBox

Source code:
    <pro:proForm HorizontalAlignment="Stretch">
        <StackPanel Orientation="Vertical" HorizontalAlignment="Left" Width="200">
            <TextBox>
                <TextBox.Text>
                    <pro:proValidationBinding Path="Text">
                        <pro:proNotEmptyStringRule/>
                    </pro:proValidationBinding>
                </TextBox.Text>
            </TextBox>
            <TextBlock Text="{pro:proReadOnlyBinding Path=Text}"/>
            <TextBlock>
                <TextBlock.Text>
                    <pro:proReadOnlyBinding Path="Text" >
                        <pro:proAddTextConverter AddText="Entered Text: " Mode="Prefix"/>
                    </pro:proReadOnlyBinding>
                </TextBlock.Text>
            </TextBlock>
            <pro:proErrorBlock Margin="5"/>
        </StackPanel>
    </pro:proForm>

Getting started

  • Run Microsoft Visual Studio
  • Create new SilverLight/WPF/WP7 project
  • Right-click on References, choose "Manage NuGet Packages"
GetStart1.png
  • Type "Prometheus.Binding" into search box and click "Install" from result window:
GetStart2.png
  • Decide to accept Licence Argeement
  • Three assemblies have been added to your project: Prometheus.TypeUtility, Prometheus.UIUtility, and Prometheus.Binding
  • Now, go to your MainPage.xaml and create namespace alias at the page declaration: xmlns:pro="http://prometheustoolkit.codeplex.com"
  • Create proForm wrapper inside your LayoutRoot control and StackPanel inside it - this will allow us to create more then one control inside our proForm:
<Grid x:Name="LayoutRoot" Background="White">
    <pro:proForm>
        <StackPanel Orientation="Vertical">
        </StackPanel>
    </pro:proForm>
</Grid>
  • First, we want TextBlock which shows us current time and Button which will update it. TextBlock binding belongs to group named "time", because we don't want to update another bindings when time is updating:
<pro:proForm x:Name="proForm">
    <StackPanel Orientation="Vertical">
        <TextBlock HorizontalAlignment="Left">
            <TextBlock.Text>
                <pro:proDelegateValueBinding BindingGroup="time" ProvideValue="provide_Time"/>
            </TextBlock.Text>
        </TextBlock>
        <Button Content="Update Time" Click="update_Time" HorizontalAlignment="Left"/>
    </StackPanel>
</pro:proForm>
private void update_Time(object sender, RoutedEventArgs e)
{
    proForm.UpdateTarget("time");
}

private void provide_Time(object sender, GenericEventArgs<object> e)
{
    e.Parameter = DateTime.Now.TimeOfDay.ToString();
}
  • Now, lets say "hello". Create TextBox and TextBlock and make TextBox TextBlock's datacontext:
<TextBox Name="txt" HorizontalAlignment="Left">
</TextBox>
<TextBlock DataContext="{Binding ElementName=txt}" HorizontalAlignment="Left">
</TextBlock>
  • Insert validation. Since we are not using data context, use proNullContextBinding which allows to validate value by validation rule:
<TextBox Name="txt" HorizontalAlignment="Left" Width="200">
    <TextBox.Text>
        <pro:proNullContextBinding>
            <pro:proNotEmptyStringRule ErrorText="Please, enter your name" />
        </pro:proNullContextBinding>
    </TextBox.Text>
</TextBox>
  • Now, bind TextBlock.Text property to TextBox.Text property (note that TextBox is in datacontext):
<TextBlock DataContext="{Binding ElementName=txt}" HorizontalAlignment="Left">
    <TextBlock.Text>
        <pro:proReadOnlyBinding Path="Text">
            <pro:proAddTextConverter AddText="Hello, " Mode="Prefix" />
        </pro:proReadOnlyBinding>
    </TextBlock.Text>
</TextBlock>
  • Set the BindingGroup of this both controls to "name" and UpdateGroupMode of TextBlock's binding to UpdateTargetWhenGroupToData. By doing this TextBlock's binding becomes updated every time someone in group tries to bring data from target to source:
<TextBox Name="txt" HorizontalAlignment="Left" Width="200">
    <TextBox.Text>
        <pro:proNullContextBinding BindingGroup="name">
            <pro:proNotEmptyStringRule ErrorText="Please, enter your name" />
        </pro:proNullContextBinding>
    </TextBox.Text>
</TextBox>
<TextBlock DataContext="{Binding ElementName=txt}" HorizontalAlignment="Left">
    <TextBlock.Text>
        <pro:proReadOnlyBinding Path="Text" BindingGroup="name"
                                UpdateGroupMode="UpdateTargetWhenGroupToData">
            <pro:proAddTextConverter AddText="Hello, " Mode="Prefix" />
        </pro:proReadOnlyBinding>
    </TextBlock.Text>
</TextBlock>
  • And finally, insert proErrorBlock. It automatically binds to proForm and shows first error from it. You can manually set to what proForm it must be bound by setting BaseControl or BaseControlName properties
  • Run the project
  • Click "Update Time" and make sure that time is updating
  • Enter your name in to TextBox and make sure that "Hello-TextBlock" is updating
  • Delete your name and make sure that error is shown
Here is working example:

Complete source code could be found here
For more information, please visit Prometheus.Binding API page.

Last edited Oct 16, 2014 at 8:11 PM by jinek, version 36

Comments

No comments yet.