How can I bind against two DynamicResources? - wpf

I am trying to bind the value of an element to a first resource , if present, then another one otherwise.
In other words, if the resources look like
<s:String x:Key="first">Hello<s:String>
<s:String x:Key="second">World<s:String>
my element's value would hold Hello
But if the resources have only
<s:String x:Key="second">World<s:String>
the value would be World
I have tried a number of solution but none seems to work or is elegant enough.
I wish I could write
<MyElement>
<MyElement.Value><MultiBinding Converter=...><DynamicResource Key=First/><DynamicResource Key=Second/> ...
where the converter takes care of finding the first non null value.
However, WPF does not allow mixing DynamicResource and MultiBinding
Do you have a solution?

Edit 1: I may have read your question a little too fast... you're binding to dynamic resources... not class properties... so the solution below is probably not what your looking for. But I'll leave it for now in case it helps you come up a solution.
Edit 2: I tried the following code in Visual Studio 2010 SP1 and it works like it should in the designer (commentout/uncomment the resource 'first'). But it fails to successfully build... which I find strange:
<TextBlock>
<TextBlock.Resources>
<!--<s:String x:Key="first">Hello</s:String>-->
<s:String x:Key="second">World</s:String>
<l:NullItemConverter x:Key="NullItemConverter" />
</TextBlock.Resources>
<TextBlock.Text>
<MultiBinding Converter="{StaticResource NullItemConverter}">
<Binding Source="{DynamicResource first}" />
<Binding Source="{DynamicResource second}"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
public class NullItemConverter : IMultiValueConverter
{
object IMultiValueConverter.Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values[0] ?? values[1];
}
object[] IMultiValueConverter.ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
Original Answer (that doesn't answer your question but may help depending on your situation):
Assuming your two properties are in the same class, could you make a third property that smartly outputs the correct value and bind to that instead:
public class MyObject : INotifyPropertyChanged
{
private string property1;
public string Property1
{
get { return this.property1; }
set
{
if (this.property1 != value)
{
this.property1 = value;
NotifyPropertyChanged("Property1");
NotifyPropertyChanged("PropertyForBinding");
}
}
}
private string property2;
public string Property2
{
get { return this.property2; }
set
{
if (this.property2 != value)
{
this.property2 = value;
NotifyPropertyChanged("Property2");
NotifyPropertyChanged("PropertyForBinding");
}
}
}
public string PropertyForBinding
{
get
{
return this.Property1 ?? this.Property2;
}
}
public MyObject() { }
#region -- INotifyPropertyChanged Contract --
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#endregion INotifyPropertyChanged Contract
}

Related

Unexpected red border (validation error) on DataGrid when selecting blank row

When I select (by clicking or by keyboard) blank row on my DataGrid (when I want to add new row), unexpected validation error occurs (but with no exception) - the border of datagrid changes to red color, as you can see on the image below. When I click second time on blank row, the red border dissapears. Everything other works fine, the new row is added. Besides, I don't have any validation rules. And when I make a row with empty text, value is valid.
I don't want this behavior and this red border, anybody knows, why this happens and how to fix it? Why and where some validation fails?
Below I append some source code:
DataGrid definition in xaml:
<DataGrid IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name"
ItemsSource="{Binding Path=ConfigFiles}" SelectedItem="{Binding Path=SelectedConfigFile}"
Grid.Column="1" Height="87" Margin="0,26,11,32" Style="{DynamicResource DataGridStyle}">
<DataGrid.Columns>
<DataGridTextColumn Width="1*" Binding="{Binding Name}" />
</DataGrid.Columns>
</DataGrid>
My ViewModel's part:
public class ManageModulesVM : BaseVM // Implements INotifyPropertyChanged
{
// ...
public ObservableCollection<ConfigFile> ConfigFiles
{
get { return selectedModule == null ? null : selectedModule.ConfigFiles; }
set
{
selectedModule.ConfigFiles = value;
OnPropertyChanged(() => ConfigFiles);
}
}
public ConfigFile SelectedConfigFile
{
get { return selectedModule == null ? null : selectedModule.SelectedConfigFile; }
set
{
if (value != null)
{
selectedModule.SelectedConfigFile = value;
}
OnPropertyChanged(() => SelectedConfigFile);
OnPropertyChanged(() => Parameters);
}
}
// ...
}
ConfigFile class:
public class ConfigFile
{
public string Name { get; set; }
public IList<Parameter> Parameters { get; set; }
public ConfigFile() { Name = ""; Parameters = new List<Parameter>(); }
}
Edit:
After further investigation I know, that SelectedItem Binding is causing problems (when I remove this binding, validation error stops to appear), but I still don't know why and how to fix this.
I've found my own solution to this question. I've written a value converter and tied it to the binding:
(SelectedItem="{Binding Path=SelectedConfigFile,Converter={StaticResource configFileConverter}}")
The converter class:
namespace Converters
{
public class SelectedConfigFileConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if(value is ConfigFile)
return value;
return null;
}
}
}
Define resource in resources.xaml file (or in any other resources place):
<ResourceDictionary (...) xmlns:conv="clr-namespace:Converters" >
<conv:SelectedConfigFileConverter x:Key="configFileConverter" />
</ResourceDictionary>
The advantage of this solution is that the SelectedConfigFile property's type did't changed (to the general object type) so it is still strongly typed.
To get the reason, when you click the new row of DataGrid in Debug mode, please see the debug window. There are first exception messages which will give you the idea why your problem is occurred.
Yes, the problem is from type casting. You need to modify the type of SelectedItem to object type as below.
public class ManageModulesVM : BaseVM // Implements INotifyPropertyChanged
{
// ...
public object SelectedConfigFile
{
get { return selectedModule == null ? null : selectedModule.SelectedConfigFile; }
set
{
if (value != null)
{
selectedModule.SelectedConfigFile = value;
}
OnPropertyChanged(() => SelectedConfigFile);
OnPropertyChanged(() => Parameters);
}
}
// ...
}
Here's a general-purpose converter you can use for any DataGrid, binding any kind of item:
public class DataGridItemConverter : MarkupExtension, IValueConverter
{
static DataGridItemConverter converter;
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (value != null && value.GetType() == targetType) ? value : null;
}
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (converter == null)
converter = new DataGridItemConverter();
return converter;
}
}
Since it implements MarkupExtension you don't even need to define a static resource, you can just reference it like this:
SelectedItem="{Binding SelectedThing,Converter={conv:DataGridItemConverter}}"
You can just add this line to your DataGrid:
<DataGrid Validation.ErrorTemplate="{x:Null}" />
You can just add this line to your DataGrid:
<DataGrid Validation.ErrorTemplate="{x:Null}" />
It will solve the problem

Silverlight Databinding Question

I have a TextBox ( TextBoxConsumer ) and i would like to enable a button in my UI when the length of the TextBox.Text is greater than 3,
i digged it down to
IsEnabled="{Binding
ElementName=TextBoxConsumer,
Path=Text.Length}"
for my button's IsEnabled Property but im not sure how to find the length and convert it to bool depending on the length of the text box how do i do it?
i would like to do it entirely in Xaml instead of code using Binding instead of code
I would prefer to use an IValueConverter class for this. I'll provide some quick code though its not exactly what you are looking for you should be able to tweak it.
In a cs file by itself:
using System;
using System.Globalization;
using System.Windows.Data;
public class IntCorrectAnswerToTrueFalseConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return (int)value > 0;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return (bool)value ? 1 : 0;
}
}
In App.xaml, add this line to the ResourceDictionary:
<app:IntCorrectAnswerToTrueFalseConverter x:Key="IntCorrectAnswerToTrueFalseConverter" />
Then in the xaml of where you use it:
<CheckBox
x:Name="answerCheckBox"
IsChecked="{Binding Score, Converter={StaticResource IntCorrectAnswerToTrueFalseConverter}}"
Click="CheckBoxChecked"/>
I did something similar using a tutorial similar to this using the INotifyPropertyChanged interface. I assume you have a model you are using for binding to the UI. You have a string member (like TextBoxConsumerString) which binds to you textbox. Now you need to add a boolean like TextBoxConsumerEnabled which you will set inside of the setter of TextBoxConsumerString and call the notify changed method.
this.OnPropertyChanged( new PropertyChangedEventArgs( "TextBoxConsumerEnabled" ) );
Here is an example:
public class TextBoxConsumerModel : INotifyPropertyChanged
{
private string _textBoxConsumerString;
public event PropertyChangedEventHandler PropertyChanged;
public string TextBoxConsumerString
{
get
{
return _textBoxConsumerString;
}
set
{
if (_textBoxConsumerString == value)
return;
TextBoxConsumerEnabled = value != null && value.Length > 3;
_textBoxConsumerString = value;
OnPropertyChanged(new PropertyChangedEventArgs("TextBoxConsumerEnabled"));
}
}
public bool TextBoxConsumerEnabled { get; set; }
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
if (PropertyChanged != null)
PropertyChanged(this, e);
}
}
That should be it as far as the model goes. Now you just need to bind to the two model properties from the XAML.

WPF datagrid Cell color depending on preivous cell value

Background.
I am developing a stock trading application.
Which obviously have a market watch.
I am developing this market watch using Datagrid.
What Does the Grid do?
It displays price points of a stock.
Every time a stock value increases the particular cell foreground turns green
if it decreases it turns red.
What i did?
I tried to use the value converter method and multibinding
Problem.
The value converter gives the current value only.
How can i pass the old value to that converter.
Code:
<wpfTlKit:DataGrid.CellStyle>
<Style TargetType="{x:Type wpfTlKit:DataGridCell}">
<Setter Property="Background">
<Setter.Value>
<MultiBinding Converter="{StaticResource myHighlighterConverter}"
>
<MultiBinding.Bindings>
<Binding RelativeSource="{RelativeSource Self}"></Binding>
<Binding Path="Row" Mode="OneWay"></Binding>
<Binding ElementName="OldData" Path="Rows"></Binding>
</MultiBinding.Bindings>
</MultiBinding>
</Setter.Value>
</Setter>
</Style>
</wpfTlKit:DataGrid.CellStyle>
Converter
public class HighlighterConverter : IMultiValueConverter
{
#region Implementation of IMultiValueConverter
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[1] is DataRow)
{
//Change the background of any cell with 1.0 to light red.
var cell = (DataGridCell)values[0];
var row = (DataRow)values[1];
var columnName = cell.Column.SortMemberPath;
if (row[columnName].IsNumeric() && row[columnName].ToDouble() == 1.0)
return new SolidColorBrush(Colors.LightSalmon);
}
return SystemColors.AppWorkspaceColor;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
#endregion
}
public static class Extensions
{
public static bool IsNumeric(this object val)
{
double test;
return double.TryParse(val.ToString(), out test);
}
public static double ToDouble(this object val)
{
return Convert.ToDouble(val);
}
}
To change the color in a DataGrid cell I recommend the following:
Build a Model that implements INotifyPropertyChanged that contains the current, and previous price plus a property that reflects the change in the price (I've attached the full model at the end of this answer).
public double ChangeInPrice
{
get
{
return CurrentPrice - PreviousPrice;
}
}
And set the Background of the CellTemplate in your DataGrid based on the change in price using a Converter.
Note: INotifyPropertyChanged helps to change the color of the cell when the price values change.
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock
Text="{Binding Path=CurrentPrice}"
Background="{Binding Path=ChangeInPrice, Converter={StaticResource backgroundConverter}}" >
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
[ValueConversion(typeof(object), typeof(SolidBrush))]
public class ObjectToBackgroundConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
SolidColorBrush b = Brushes.White;
try
{
double stock = (double)value;
if (stock > 0)
{
b = Brushes.Green;
}
else if (stock < 0)
{
b = Brushes.Red;
}
}
catch (Exception e)
{
}
return b;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
Here is full model for completeness:
public class Stock : INotifyPropertyChanged
{
public Stock(string stockName, double currentPrice, double previousPrice)
{
this.StockName = stockName;
this.CurrentPrice = currentPrice;
this.PreviousPrice = previousPrice;
}
private string _stockName;
public String StockName
{
get { return _stockName; }
set
{
_stockName = value;
OnPropertyChanged("StockName");
}
}
private double _currentPrice = 0.00;
public double CurrentPrice
{
get { return _currentPrice; }
set
{
_currentPrice = value;
OnPropertyChanged("CurrentPrice");
OnPropertyChanged("ChangeInPrice");
}
}
private double _previousPrice = 0.00;
public double PreviousPrice
{
get { return _previousPrice; }
set
{
_previousPrice = value;
OnPropertyChanged("PreviousPrice");
OnPropertyChanged("ChangeInPrice");
}
}
public double ChangeInPrice
{
get
{
return CurrentPrice - PreviousPrice;
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
well i think the problem is not the datagrid, but the object you bind to. if you bind to a datatable the oldvalues are built in (DataRowVersion). if you have other entity objects, then this entities need to support the original and modified values.

Databinding to the value of a multiBinding

Maybe I am not quite grasping multibindings.
I have a property on my viewmodel called OfficeDisplayName that is written to the database.
This is a concatenated field based on a person's FirstName, Lastname, and office location.
So I have a multibinding on a textBlock...no biggie...works beautifully...but how do I bind the full value of this concatenation to the OfficeDisplayName property? Do I have to have a hidden element that binds to the multibound textbox? I have seen several examples that are almost what I need, but just dont answer the concat databinding question.
One way is to let the textblock bind directly to OfficeDisplayName and then put the concatenation logic in the OfficeDisplayName property on your viewmodel instead of in the MultiValueConverter. So when ever one of the properties FirstName, LastName, or office location change you'd fire the PropertyChanged event for OfficeDisplayName - i.e. something along the following lines. This way you will not need a converter at all:
class YourViewModel : ViewModel
{
string _firstName;
public string FirstName
{
get { return _firstName; }
set
{
if (_firstName != value)
{
_firstName = value;
OnPropertyChanged("FirstName");
OnPropertyChanged("OfficeDisplayName");
}
}
}
// More properties here
// ...
public string OfficeDisplayName
{
get { return String.Join(" ", new string[] { _firstName, _lastName, _officeLocation}); }
}
}
Another way is to pass your viewmodel itself as a parameter to your MultiValueConverter. In your converter you can set the value of OfficeDisplayName directly. I think this way is a bit "hack-ish" but it is a matter of taste. Your code would look like the following:
The binding in XAML:
<MultiBinding Converter="{StaticResource theConverter}" Mode="OneWay">
<Binding /> <!-- Pass the datacontext as the first parameter -->
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
The converter:
class TheMultiValueConverter : IMultiValueConverter
{
#region IMultiValueConverter Members
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var viewModel = values[0] as TheViewModel;
var ret = String.Join(" ", values.Skip(1).Cast<string>().ToArray());
viewModel.OfficeDisplayName = ret;
return ret;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}

Current Date in Silverlight XAML TextBlock

I am coming from Flex where you can do just about anything inside of curly braces. I am trying to get a TextBlock to display today's Date and Time without just coding it in C#. I have tried many different variations of the following with no luck.
TextBlock Text="{Source=Date, Path=Now, StringFormat='dd/MM/yyyy'}"
I know I could probably just set a property MyDate and bind to that but why can't I bind directly to the DateTime.Now property?
Binding in Silverlight requires a Source object or a Dependency object. From that source object you can bind to Properties (hence by definition you are binding to instance members) or Dependency Properties.
Since DateTime.Now is a static property you cannot bind to it in Silverlight directly, hence some code is needed. The next best thing is to use code to:-
ensure as much of what you need can be expressed in XAML
to do so in an as de-coupled manner as possible.
Hence we can analyse that we need two things.
Expose the static members of DateTime as instance properties of some object
Have some way to format the DateTime to a desirable output.
To handle the first item I would create a StaticSurrogate class, where I would create instance properties for the static properties that we need access to:-
public class StaticSurrogate
{
public DateTime Today { get { return DateTime.Today; } }
public DateTime Now { get { return DateTime.Now; } }
}
Now we need a way to format a Date time. A value converter is the right tool for this job, borrowing heavily from this Tim Heuer Blog :-
public class FormatConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (parameter != null)
{
string formatterString = parameter.ToString();
if (!String.IsNullOrEmpty(formatterString))
{
return String.Format(culture, String.Format("{{0:{0}}}", formatterString), value);
}
}
return (value ?? "").ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
With these two classes in place we can now do the rest in Xaml, first we need instances of these classes in our resources:-
<UserControl.Resources>
<local:StaticSurrogate x:Key="Static" />
<local:FormatConverter x:Key="Formatter" />
</UserControl.Resources>
Now we can wire up the TextBlock :-
<TextBlock Text="{Binding Today, Source={StaticResource Static},
Converter={StaticResource Formatter}, ConverterParameter='dd MMM yyy'}" />
Note that this approach has the following advantages:-
we do not need to add code to the UserControl on which the TextBlock is placed, nor do we have to fiddle around with any data context.
The Static resources could be placed in the App.Resources which would make the creation of the TextBlock entirely independent of having to add anything else to the UserControl.
The formatting used to display the date can be independently modified.
Access to additional static properties can easily be added to the StaticSurrogate class.
Even if you could declare DateTime.Now in Silverlight's XAML (since you can in WPF - http://soumya.wordpress.com/2010/02/12/wpf-simplified-part-11-xaml-tricks/), you have the issue that your time won't update. If you use a local timer that updates on the second you can ensure that your time will update as well.
public class LocalTimer : INotifyPropertyChanged
{
private DispatcherTimer timer;
public LocalTimer()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromSeconds(1.0);
timer.Tick += new EventHandler(TimerCallback);
this.TimeFormat = "hh:mm:ss";
this.DateFormat = "dddd, MMMM dd";
}
private void TimerCallback(object sender, EventArgs e)
{
PropertyChanged(this, new PropertyChangedEventArgs("FormattedDate"));
PropertyChanged(this, new PropertyChangedEventArgs("FormattedTime"));
}
public bool Enabled
{
get { return this.timer.IsEnabled; }
set { if (value) this.timer.Start(); else this.timer.Stop(); }
}
public string FormattedDate { get { return DateTime.Now.ToString(this.DateFormat); } set {} }
public string FormattedTime { get { return DateTime.Now.ToString(this.TimeFormat); } set{} }
public string TimeFormat { get; set; }
public string DateFormat { get; set; }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
}
Declare an instance of this in xaml ala:
<local:LocalTimer x:Key="theTime" Enabled="True" />
and use the binding to ensure that your time is always reflected.
<TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedDate, Mode=OneWay}" x:Name="TodaysDate" />
<TextBlock Text="{Binding Source={StaticResource theTime}, Path=FormattedTime, Mode=OneWay}" x:Name="CurrentTime" />
xmlns:sys="clr-namespace:System;assembly=mscorlib"
Text="{Binding Source={x:Static sys:DateTime.Today}, StringFormat='Today is {0:dddd, MMMM dd}'}"

Resources