|
I reproduced your code but it works fine here without any issues. Have you inserted this code inside a resources tag? Eg:
<Grid.Resources>
<XmlDataProvider x:Key="MoreColors" XPath="/colors">
<x:XData>
<colors xmlns="" >
<color name="pink"/>
<color name="white"/>
<color name="black"/>
<color name="cyan"/>
<color name="gray"/>
<color name="magenta"/>
</colors>
</x:XData>
</XmlDataProvider>
</Grid.Resources>
|
|
|
|
|
I almost never use ToString(), but when I do, its for a representation of the entire object. The DataTemplate is binding to a single property (or multiple ones with a value converter).
|
|
|
|
|
DataTemplate is actually often used for the represantation of the entire object, arbitrarily complex (both the object and the representation).
|
|
|
|
|
If I only need to show 1 value in my combobox I normally just set the DisplayMemberPath of the combobox to the property I want to show.
It is said that your life flashes before your eyes just before you die. That is true, it's called Life. - Terry Pratchett
|
|
|
|
|
I would say that ToString "converts" the whole object to a string, simple as that. Which means 1) you can't return any fancy colorful represantation other than a simple string and 2) the ToString() is global application-wise, meaning that you can't make two comboboxes displaying the same list of items make display slightly different things (which you can do with DataTamplates).
DataTemplates are much more versatile, can be specific for a particular control (combobox) or used globally. They can also display much more complex data than just a string.
Then there are value converters...
The call is yours, ToString may be just fine for simple combobox item represatation, setting ItemTemplate doesn't hurt either.
|
|
|
|
|
I wasn't wrong, just not really answering your question, sorry for that. I just wanted to outline the main differences between the two approaches, which is not really what you asked. Ad 1) sure, VM does not return colors, what I had in mind was a rather more complex object than just DisplayName and then ToString() forces you to condense all the data to one string, which can be very limiting and styling is of no help. Ad 2) of course you can use DataTemplates but I was talking about limitations of ToString().
|
|
|
|
|
Not an answer to your question but a comment. You mention Label as a means to display a string, I'll suggest you use TextBlock instead as Label is not much more than a wrapper around TextBlock . Label Only adds the ability to add a border and using an access key, both you can do with out using a Label .
See Josh Smiths discussion about Label and TextBlock Differences between Label and TextBlock[^]
|
|
|
|
|
You may find that this thread[^] covers what's happening and suggests an appropriate solution (the TabControlEx code).
|
|
|
|
|
I want to display a license version in a TextBox from a property and do it with bindings.
There are two buttons to switch between different license versions and a TextBox to display the license version.
Here is the XAML code:
<Window x:Class="WpfApplication1.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">
<StackPanel Orientation="Vertical">
<TextBox Name="txtVersion" Text="????"></TextBox>
<Button Content="Version 1" Click="Version1_Click"></Button>
<Button Content="Version 2" Click="Version2_Click"></Button>
</StackPanel>
</Window>
Notice the TextBox Text property, which I don't know how to bind.
The C# code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
License license1;
License license2;
public MainWindow()
{
InitializeComponent();
license1 = new License("Version 1");
license2 = new License("Version 2");
License.Current = license1;
this.DataContext = this;
this.PropertyChanged += new PropertyChangedEventHandler(MainWindow_PropertyChanged);
}
void MainWindow_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
}
private void Version1_Click(object sender, RoutedEventArgs e)
{
License.Current = license1;
PropertyChanged(this, new PropertyChangedEventArgs("Version"));
}
private void Version2_Click(object sender, RoutedEventArgs e)
{
License.Current = license2;
PropertyChanged(this, new PropertyChangedEventArgs("Version"));
}
}
class License
{
static License current;
string version;
public License(string version)
{
this.version = version;
}
public static License Current
{
get { return current; }
set { current = value; }
}
public string Version
{
get { return version; }
}
}
}
The class License has the static instance Current property which always give me access to the current Licence. With this I can access the current version like: License.Current.Version
So how do I bind the TextBox's Text to that?
I try also to display the version number every time I press either of the buttons. Is this a working solution?
|
|
|
|
|
Your binding simply needs to be "{Binding Version}" to bind to the version, providing you add a Version property into your window, e.g.
public string Version { get { return License.Current.Version; } }
|
|
|
|
|
That doesn't work. Crashes with an InvalidOperationException. "A TwoWay or OneWayToSource binding cannot work on the read-only property 'Version' of type 'WpfApplication1.MainWindow'."
I changed the property in the Window and it worked just fine.
public string Version
{
get { return License.Current.Version; }
set { string s = value; }
}
But I'm looking for the WPF binding to reach the Version Property in the License class through the static instance Current of License class. Instead of bind to my Window class.
If it's possible...
|
|
|
|
|
Mc_Topaz wrote: "A TwoWay or OneWayToSource binding cannot work on the read-only property 'Version' of type 'WpfApplication1.MainWindow'."
I changed the property in the Window and it worked just fine.
Wrong fix... Don't put in a dummy setter... Just change the binding to: {Binding Version,Mode=OneWay}
But anyway, you CAN bind to static classes, but you might have trouble with an inner class...
Example:
<TextBlock Text ="{Binding Source={x:Static local:MyStaticClass.MyProperty}}"/>
(From here[^])
|
|
|
|
|
Thanks, I changed the XAML.
But I don't use a static class. I use a static INSTANCE of the same class to reach a property within the class which is not static.
|
|
|
|
|
Hmm, good point... Need more caffeine... So Pete's solution is the best bet, with that OneWay tweak.
|
|
|
|
|
Sorry, I should have mentioned that you would set your binding as Mode=OneWay . As it currently stands, your code cannot do this because License is not a property in your DataContext. This means that while your code-behind can see it, the XAML cannot. To overcome this from your main window, you need to expose a property that can be bound to.
|
|
|
|
|
Allright, that was good to know. Thanks!
|
|
|
|
|
Hi,
I have a Combobox, who gets Gradient blue color when mouse-over or clicked. I managed to change the color of selecting Items background, but the mouse over background color couldn't change.
<Style x:Key="ComboStyle" TargetType="{x:Type ComboBox}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ComboBox}">
<Border Name="bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding Background}" Padding="2">
<ContentPresenter />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="bd" Property="Background" Value="#F7F7F7" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
On adding the above code, MouseOver is definetely achieved i.e. Background color is changed on MouseOver. But with this, I can't see the drop down triagle icon or click the combobox that shows the drop down.
What am I lacking and ho to achieve it ? Any help is highly appreciated.
Thanks & Regards,
|
|
|
|
|
When you set the Template property, you're basically replacing the entire control. In this case, you turned a ComboBox into a ContentControl by removing (not including) all of the things that made it a ComboBox.
If all you want to change is the Background, then you should be putting this stuff directly in the Style.Triggers, not inside a Template.
For example:
<Style TargetType="ComboBox">
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Background" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
|
|
|
|
|
Hi,
I am working on canvas and need to implement infinite scrolling towards the top and bottom.
I have a grid which contains the scrollviewer whose content is the custom canvas. The canvas contains some custom objects which have been drawn over the canvas and bears the drag and drop functionality.
When I drag any object towards right/bottom, scroll bars appear and gives space to put the object.
However, this functionality is not there for top/left scrolling. So, when a object is dragged towards left/top and it reaches the end, the canvas is not resized in order to accomodate the new position of object.
I know, its because canvas has a origin as 0,0 and width and height as infinite.
Currently, user has to 'Select All' all the displayed objects in the canvas and drag towards the right/bottom in order to create a new object towards left/top of it.
Searched and found this link http://www.eggheadcafe.com/software/aspnet/32319058/wpf--scroll-canvas-to-objects-beyon-top-or-left-side.aspx[] of some help, unfortunately unable to mimic the behavior in my application.
Is the use of MeasureOverride() and ArrangeOverride() method is appropriate in this situation?
The best example for infinite scrolling towards right/bottom is Excel sheet. I am looking for the same behavior for left and top.
Please let me know for any clarifications.
Thanks in advance!!!
Praveen
|
|
|
|
|
Hi,
I think the key to solve your problem could be the IScrollInfo interface. Your canvas implementation would override MeasureOverride() to calculate min and max values, ArrangeOverride() to position the items considering the scroll offsets, and implement IScrollInfo to sync the scroll offsets and constraints with the ScrollViewer.
Hope that helps.
|
|
|
|
|
Hello Experts,
I want the user to use the address only once in the browser.
For ex - if www.Xtree.com is the website name, then the user uses it in the address bar & it loads the page.Now if the user opens the page again in another tab of the browser it must throw Message that is already been used. This applies even if he browses it with other browser..
Please help me with this...
Regards,
Satish
|
|
|
|
|
What has this got to do with this forum?
|
|
|
|
|
|
Hi,
Can anyone tell me how to place a multiple window controls inside one window controls in WPF.
I have to use 4 window controls or some controls like that inside one window control, so that later i can provide the drag and drop functionality of that sub window controls for the user.
I have written a code like this, it is showing exactly what i want at design time, but at runtime it is throwing xmlparseexception, i dint get the reason for this, can u tell me what is the wrong in the below code or is it possible to place a multiple window controls inside one window control..
This is my code..
<Window x:Class="TWS_Demo_Project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="TWS" Height="Auto" Width="Auto">
<Grid Height="Auto" Width="Auto">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Window Title="Order Entry" Grid.Row="0" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Name" Grid.Row="0" Grid.Column="0"/>
<TextBox Name="txtname" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Password" Grid.Row="1" Grid.Column="0"/>
<TextBox Name="txtpassword" Grid.Row="1" Grid.Column="1"/>
</Grid>
</Window>
<Window Title="Parked Orders" Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Height="Auto" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Text="BrokerID" Grid.Row="0" Grid.Column="0"/>
<TextBox Name="txtbrkid" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Country" Grid.Row="1" Grid.Column="0"/>
<TextBox Name="txtcountry" Grid.Row="1" Grid.Column="1"/>
</Grid>
</Window>
<Window Title="Pending Trades" Grid.Row="1" Grid.Column="0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="Auto" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Pending Trade Name" Grid.Row="0" Grid.Column="0"/>
<TextBox Name="txtpndid" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Pending Trade Details" Grid.Row="1" Grid.Column="0"/>
<TextBox Name="txtpnddetails" Grid.Row="1" Grid.Column="1"/>
</Grid>
</Window>
<Window Title="Rejected Orders" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Top" Height="Auto" Width="Auto">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="150"/>
<ColumnDefinition Width="150"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Rejected Feed" Grid.Row="0" Grid.Column="0"/>
<TextBox Name="txtrjdfeed" Grid.Row="0" Grid.Column="1"/>
<TextBlock Text="Rejection Reason" Grid.Row="1" Grid.Column="0"/>
<TextBox Name="txtrjdreason" Grid.Row="1" Grid.Column="1"/>
</Grid>
</Window>
</Grid>
</Window>
|
|
|
|
|
You can't add a Window as the child of another Window. A window must be the root control. If I were you and you wanted to keep the headers for each section, I would change Window to GroupBox, something like this:
<GroupBox Header="Order Entry"...
<GroupBox Header="Parked Orders"...
<GroupBox Header="Pending Trades"...
<GroupBox Header="Rejected Orders"
You could also use UserControls, and then add another Label to each to display the header / title.
Hope this helps
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|