|
Use a hierarcial data template
Everything makes sense in someone's mind
|
|
|
|
|
Xaml:
<sdk:datagridtextcolumn foreground="{Binding Path=Age, Converter={StaticResource ContactListViewModel}, ConverterParameter=GetAgeColor}"
="" binding="{Binding Path=Age}" canuserreorder="True" canuserresize="True" canusersort="True" width="Auto" header="Age">
Converter (in ViewModel):
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
switch(parameter.ToString())
{
case "GetAgeColor":
//Some code
return new SolidColorBrush(Colors.Red);
}
return value;
}
=============
Here Age is type double, I have converter to return different colors of Brushes based age. I'm getting the following error during the loading of the view in InitializeComponent(); Please help, what I'm doing wrong?
{System.ArgumentException: Object of type 'System.Windows.Data.Binding' cannot be converted to type 'System.Windows.Media.Brush'.
at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast)
at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr)
at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
at System.Reflection.RuntimePropertyInfo.SetValue(Object obj, Object value, Object[] index)
at MS.Internal.XamlMemberInfo.SetValue(Object target, Object value)
at MS.Internal.XamlManagedRuntimeRPInvokes.SetValue(XamlTypeToken inType, XamlQualifiedObject& inObj, XamlPropertyToken inProperty, XamlQualifiedObject& inValue)}
|
|
|
|
|
When you return a color using a converter, you need to set it to the BackGround or the Foreground, not the Binding.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Sorry I didn't paste the code completely. Please heck this
<sdk:datagridtextcolumn <b=""><foreground="{binding path="Age," converter="{StaticResource" contactlistviewmodel},="" converterparameter="GetAgeColor}"" <="" b="">
Binding="{Binding Path=Age}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Age"/>
|
|
|
|
|
Oops! it again got truncated because I posted in HTML format. Here is right xmal.<sdk:DataGridTextColumn Foreground="{Binding Path=Age, Converter={StaticResource ContactListViewModel}, ConverterParameter=GetAgeColor}" Binding="{Binding Path=Age}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Age"/>
|
|
|
|
|
You are binding your foreground and your main value to Age.
ananddayalan wrote: Binding="{Binding Path=Age}"
ananddayalan wrote: Foreground="{Binding Path=Age, Converter={StaticResource ContactListViewModel}, ConverterParameter=GetAgeColor}"
Age is a media color which you cannot bind a value to.
That must be the issue.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
I came to know DataGridTextColum.Foregorund is not a dependency property. Refer here for more details -> http://forums.silverlight.net/forums/p/207738/488966.aspx#488966
Thanks a lot. I achieved same by using data template.
<sdk:DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBlock Text="{Binding Height}" Foreground="{Binding Path=Height, Converter={StaticResource ContactListViewModel}, ConverterParameter=GetHeightColor}"></TextBlock> </DataTemplate> </sdk:DataGridTemplateColumn.CellTemplate>
<sdk:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Height}" Foreground="{Binding Path=Height, Converter={StaticResource ContactListViewModel}, ConverterParameter=GetHeightColor}"></TextBlock>
</DataTemplate>
</sdk:DataGridTemplateColumn.CellTemplate>
|
|
|
|
|
You'd probably find it simpler to create a distinct class for your converter, import the namespace and declare a static resource - your binding expression then simply becomes:
Foreground="{Binding Path=Age, Converter={StaticResource AgeToBrushConverter}}"
C# has already designed away most of the tedium of C++.
|
|
|
|
|
Is it possible to create a Windows Explorer like application in Silverlight?
I want my application to show the drive information, files & folders inside the drive.
Is it possible? If so, how? Please share me some good links. Thanks in advance.
|
|
|
|
|
MichaelGaudioso wrote: Is it possible to create a Windows Explorer like application in Silverlight?
Yes, it is possible. But it needs to be a Silverlight OOB Application.
MichaelGaudioso wrote: I want my application to show the drive information, files & folders inside the drive.
Is it possible?
Again yes, it is possible. I already have an article on the same.
Read it @ CodeProject: File Explorer using Silverlight 4 COM Interoperability[^]
|
|
|
|
|
|
Yes but only in Silverlight 3 and above.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
How to implement a crystal report in a WPF application
|
|
|
|
|
Is there even a viewer for CR in SL. As reporting services has not even a viewer < $500 I'd be surprised if CR has one out yet.
I'm looking at calling out to an asp page in the web project with the RS asp viewer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Check this[^] out.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Hi,
I've just created a trivial xaml window that databinds a textbox to a property:
<Window x:Class="WpfTests.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="root">
<StackPanel>
<DockPanel>
<Button DockPanel.Dock="Right" Height="23" Click="buttonTest_Click">Browse...</Button>
<TextBox DockPanel.Dock="Left"
Height="23" Margin="0,0,2,0" Name="textBoxFileName"
Text="{Binding ElementName=root, Mode=TwoWay, Path=FileName}"/>
</DockPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding ElementName=root, Mode=TwoWay, Path=FileName}"></TextBlock>
<Button DockPanel.Dock="Bottom" Name="buttonToggle" Click="buttonToggle_Click">Toggle...</Button>
</DockPanel>
</StackPanel>
</Window>
And the (trivial) code behind:
namespace WpfTests
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(MainWindow));
public string FileName
{
get { return (string)GetValue(FileNameProperty); }
set { SetValue(FileNameProperty, value); }
}
private void buttonTest_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog();
if (d.ShowDialog() == true)
this.FileName = d.FileName;
}
private void buttonToggle_Click(object sender, RoutedEventArgs e)
{
this.FileName = "Toggle clicked.";
}
}
}
I have a couple of questions (that require simple 'assume-I-know-nothing' answers):
To date, this is the only trivial example I've found of binding the value of a control to a class property. Am I doing this the correct way, or is this something fundamental to databinding that I'm missing?
If, in VS2010, I go to the xaml view, select the 'Text' property of 'textBoxFileName' and 'Apply Data Binding', it auto-navigates itself to 'FileName' (that I set), but then hovering over the value 'FileName' the tooltip says 'Path item FileName cannot be resolved': should I be concerned, or is this just the xaml editor knowing nothing about the code behind? Or is there an error or flaw in the code or design?
Thanks in advance,
Barry.
Regards,
Barry
|
|
|
|
|
Hi Barry,
This is by design. Designer never look for the code behind of your current class. If you want provide designer support, move all your property declaration from your current class to another class and inherit to your current class.
For example, create extended window with your property like below mentioned.
<br />
public class ExtendedWindow : Window<br />
{<br />
public static readonly DependencyProperty FileNameProperty = DependencyProperty.Register("FileName", typeof(string), typeof(ExtendedWindow));<br />
public string FileName<br />
{<br />
get<br />
{<br />
return (string)GetValue(FileNameProperty);<br />
}<br />
set<br />
{<br />
SetValue(FileNameProperty, value);<br />
}<br />
} <br />
}<br />
<br />
Now inherit this extended class to your MyWindow class
<br />
public partial class MainWindow : ExtendedWindow<br />
{<br />
public MainWindow()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
<br />
private void buttonTest_Click(object sender, RoutedEventArgs e) <br />
{ <br />
Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog();<br />
if (d.ShowDialog() == true) <br />
this.FileName = d.FileName;<br />
} <br />
private void buttonToggle_Click(object sender, RoutedEventArgs e) <br />
{ <br />
this.FileName = "Toggle clicked."; <br />
}<br />
}<br />
Xaml
<local:ExtendedWindow x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="525"
x:Name="root">
<StackPanel>
<DockPanel>
<Button DockPanel.Dock="Right" Height="23" Click="buttonTest_Click">Browse...</Button>
<TextBox DockPanel.Dock="Left"
Height="23" Margin="0,0,2,0" Name="textBoxFileName" Text="{Binding ElementName=root, Mode=TwoWay, Path=FileName}"/>
</DockPanel>
<DockPanel>
<TextBlock DockPanel.Dock="Bottom" Text="{Binding ElementName=root, Mode=TwoWay, Path=FileName}"></TextBlock>
<Button DockPanel.Dock="Bottom" Name="buttonToggle" Click="buttonToggle_Click">Toggle...</Button>
</DockPanel>
</StackPanel>
</local:ExtendedWindow>
Now property editor will identify FileName property without any issue.
MVVM devotee
|
|
|
|
|
Venugopalm - thanks for the reply, this is helpful!
Regards,
Barry
|
|
|
|
|
Does anyone have a decent example of loading formatted data from SQL and binding it to the RichTextBox.
I can get the data in via
Run Text="{Binding Path=EditableRoom.RoomNotes,Mode=TwoWay}"
but it is the unformatted xml text.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I could not find a sample for what you are looking for.
What you might need to consider, however, is the use of value converters.
For more info, see here[^].
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Abhinav S wrote: is the use of value converters
Yeah I have the nasty feeling that is going to be a requirement, I'm astonished there are no binding samples, I would think binding for any control a basic requirement
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I came across a converter that was doing some paragraph formatting for a rich text box. That was the closest I got to an example.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
Visit the Hindi forum here.
|
|
|
|
|
Any link, the only one I have found is for WPF and the xml reference used is not valid for silverlight.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
We have a WPF/XBAP application that the user downloads and executes once authenticating through an ASP.NET portal. After authenticating, ASP.NET passes the user's information (and some other configuration information) using a temporary cookie(s). The XBAP then uses this information to authenticate with a WCF service that the XBAP uses until the XBAP is closed by the user.
Of course the security hole here is that someone could potentially retrieve the cookie information from memory before the cookies are deleted, load the XBAP from the click-once cache, and put that cookie information in IE for the XBAP to retrieve. Of course the steps to do that are a little more complicated then what I mentioned, but you should get the idea.
Does anyone have any ideas on how the XBAP and it's communication between ASP.NET and the WCF service could be made more secure? I'm trying to think like a "hacker" so we can make this solution more secure, but I'm running out of options in my thought process.
We've thought about requiring the user to also authenticate using a login screen in the XBAP, but all of us know what kind of reaction we would get from users if they had to login twice.
|
|
|
|
|
We have a website and have a very complex page need to be developed. The page would contain a lot of datagrids and images that would be almost impossible to render without a page crash if we develop the page as it is. So it was suggested to develop the page in WPF instead of asp.net and then integrate it with asp.net. That is on click of a menu item it will launch a WPF UI.
Is it a possible way of doing as I am totally new to WPF?
|
|
|
|
|