|
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?
|
|
|
|
|
You would do better looking at SilverLight. It is certianly possible - performance is up to you though.
|
|
|
|
|
Oh sorry. I was asking if it is possible or not in WPF to do the functionality. is it possible or not? I don't intent to make this thread to discuss silverlight.
|
|
|
|
|
He wasn't he was suggesting that you use Silverlight as it is a tool that is more suited to your problem.
WPF Controls in a asp.net page
This might be a better starting place for you
As barmey as a sack of badgers
Dude, if I knew what I was doing in life, I'd be rich, retired, dating a supermodel and laughing at the rest of you from the sidelines.
|
|
|
|
|
You can run a WPF app within a browser - look up XBAP but really from the brief spec you have provided SilverLight would be preferable. Functionality and performance is up to you.
|
|
|
|
|
Thanks! The choice of WPF was to avaoid the browser rendering time as the time is consumed more while rendering. So is it wise to create a WPF app and use libraries like XBAP?
|
|
|
|
|
The problem with XBAP's is that they require a certificate, and those cost money. Silverlight might be the way to go if it's all you need. If you converting a web page (e.g., Javascript, HTML, CSS) to something, then Silverlight should suffice. Silverlight supports things that will help performance, such as virtualization, though there are some differences as compared to WPF.
|
|
|
|
|
Do you only intend to target Windows IE customers? If so, you can use an XBAP; if not, you need to look at Silverlight.
|
|
|
|
|
Hi,
Yes. Just go ahead with XBAP application which enables your application as desktop application without any performance issue.
MVVM devotee
|
|
|
|
|
If you are fine with XBAP, you might as well use ClickOnce and deploy a WPF desktop app. That'd be a far more convenient approach.
|
|
|
|
|
hello,
i have trouble with the wpf toolkit accordion control.
i have a UserControl which creates a dynamic nested accordion control (3 levels).
when the 3rd level accordionItem is clicked a new usercontrol, also a accordion but in XAML, will be added to the content of the 3rd level accordionItem.
both usercontrols have a link to a ResourceDictionray (Theme) like this:
xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Layout.Toolkit">
<Grid Name="MainGrid">
<Grid.Resources>
<ResourceDictionary Source="../View/Themes/CustomAccordion.xaml"></ResourceDictionary>
</Grid.Resources>
</Grid>
the problem is, that the XAML accordion display my theme configuration but the dynamic created accordion won't display the same theme.
does anybody know why?
thanks for any help
|
|
|
|
|
i checked my code and found the problem
accordionItem.SetResourceReference
was set and does override my ResourceDictonary Source
|
|
|
|
|
Hi,
I am working with a WPF project and have a string property Address.
StackPanel Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Margin="2,2,10,2" Visibility="{Binding Address, Converter = {StaticResource FormatAddress}}"
The converter FormatAddress is returning Visibility.Visible if Address is !string.Empty and !="".
Can I use this converter if I want show the StackPanel if the Address is string.Empty or ""? Or do I have to create a new converter for this case?
Regards
Olof
|
|
|
|
|
Why not just use string.IsNullOrWhitespace in the converter to decide whether the string is empty or not?
|
|
|
|
|
He can, provided he is using .Net 4.0.
As far as I remember (from a previous discussion), this does not exist in versions before 4.0.
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.
|
|
|
|
|
True, but it's easy to replicate the logic.
public static bool IsNullOrWhitespace(this string text)
{
if (string.IsNullOrEmpty(text))
return true;
return text.Trim() == string.Empty;
}
|
|
|
|