|
i try to change the size of text in ritchbox with code behind
TextRange tr = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
tr.ApplyPropertyValue(FontSizeProperty, (double)9);
but i want to change
tr.ApplyPropertyValue(FontSizeProperty, (double)9) by
double size=Convert.ToDouble(comboboxfontsize.selectedItem)
tr.ApplyPropertyValue(FontSizeProperty, size)
i get an exception
Impossible d'effectuer un cast d'un objet de type 'System.Windows.Controls.ComboBoxItem' en type 'System.IConvertible'.
so how can i get the value selected item?
|
|
|
|
|
I don't know how you are populating the ComboBox, but I assume it is in the XAML. In this case the SelectedItem would be a ComboBoxItem which does not implement the IConvertible interface and so cannot be converted to a double. Personally, I would use WPF's binding mechanism to implement this using 2 properties, and ObservableCollection<int> to hold the allowed font sizes, and an int property to hold the selected font size, something like this:-
public ObservableCollection<int> Fonts
{
get
{
return new ObservableCollection<int>() { 8, 9, 10, 12, 14 };
}
}
int selectedFontSize;
public int SelectedFontSize
{
get
{
return selectedFontSize;
}
set
{
if (selectedFontSize != value)
{
selectedFontSize = value;
OnPropertyChanged("SelectedFontSize");
}
}
}
(this assumes you user control / window implements INotifyPropertyChanged ). Then you can just create the bindings in your XAML like this:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
</Grid.RowDefinitions>
<ComboBox ItemsSource="{Binding Fonts}" SelectedItem="{Binding SelectedFontSize}" Width="200" HorizontalAlignment="Left"/>
<RichTextBox Grid.Row="1">
<FlowDocument FontSize="{Binding SelectedFontSize}">
<Paragraph >
Hello World!
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
and that is all there is to it, not forgetting to set the DataContext of your user control / window to itself like this in the constructor:
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
}
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
|
|
|
|
|
Hi,
i have a wpf user control wich allow me to show print window .
how can i show wpf user control when i click on button in another window??
|
|
|
|
|
Place it in a window and display it on button click.
private void myButton_Click(object sender, EventArgs e)
{
Window myWindow = new Window
{
Content = new myUserControl()
};
window.ShowDialog();
}
|
|
|
|
|
Hello Everyone
I have this slight problem maybe someone can help me please...
I have created a WPF project on VS2010. In this project I have a window with one DataGrid (on the left side of the window), seven TextBoxes, two ComboBoxes and one CheckBox (on the right side of the window).
When I run application, on the DataGrid I display only the name of employee from the database.
My Question:
How can I display all the details of the employee in the textboxes, comboboxes, and checkbox when I click on the cell of the datagrid?
Could someone help me on this please I'm new to WPF...
Kind Regards to everyone
Lapeci
|
|
|
|
|
You could consider presenting your data in a ListBox or a ListView and set IsSyncronisedWithCurrentItem="True" using the autamatic Parent Child binding in WPF.
|
|
|
|
|
Is any way to do it with datagrid rather then with listbox, and is it possible to show me with some sample code according to my question please...
kind regards
lapeci
|
|
|
|
|
I don't know any quick way to do it with Grid, but you can get a very similar look by using a list view - maybe something along these lines: Other than that wait for some one else to come up with the answer. Or read up on Binding.
<ListView Name="lvwCommandLog"
ItemsSource="{Binding Path=CommandLog, Mode=TwoWay}"
MouseDoubleClick="lvwCommandLog_MouseDoubleClick"
ToolTip="Double click to run this command again" >
<ListView.View >
<GridView >
<GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name, Mode=TwoWay}" ></GridViewColumn>
<GridViewColumn Header="Age" DisplayMemberBinding="{Binding Age}" ></GridViewColumn>
<GridViewColumn Header="Time" DisplayMemberBinding="{Binding Path=Time, StringFormat=\{0:h\\:mm\\:ss\}}"></GridViewColumn>
</GridView>
</ListView.View>
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Do Something" Click="MenuItem_Click" />
</ContextMenu>
</ListView.ContextMenu>
</ListView>
<Text Text="{Binding Name}"/>
|
|
|
|
|
There are two ways to do this:
1. Bind the DataGrid's SelectItem to a property in your ViewModel (or code behind if you're not using MVVM ), then in the setter for that property populate properties for the TextBoxes, ComboBoxes etc.
This method has the advantage of being able to add a Cancel button.
2. Bind the TextBoxes, ComboBoxes etc to the DataGrid's SelectedItem property. Something like this:
{Binding ElementName=DataGrid1, Path=SelectedItem.TextPropertyName}
This method saves you a bit of coding but doesn't offer an easy way to cancel changes.
|
|
|
|
|
I create a library of these classes contains a user control that contains images as background to some button, I add it to the main page such as referance, I added this code to retrieve the control xaml user
<Window x: Class = "WpfApplication3.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"
xmlns: usercontrol = "clr-namespace: usercontrol; assembly = usercontrol">
<Grid>
<usercontrol:myeditor> </ usercontrol: myeditor>
</ Grid>
</ Window>
but i get an exception: the user control Cannot locate resource
|
|
|
|
|
What are the values of the 'Build Action' and 'Copy to Output' properties of the background image (right click - properties)?
What does the xaml (or code) look like for assigning the image as the background of the UserControl look like?
|
|
|
|
|
Your error is going to be in the myeditor usercontrol. Like Jeremy suggested check to ensure that your image sources on the control are correct, and that the control is able to locate them during runtime.
|
|
|
|
|
Hi guys,
I'm working for the first time in WPF with MVVM pattern and I have a datagrid that is populated according to search terms in textbox on top. Right now, I'm able to populate the grid with all the data but I'm not able to get the value of the TextBox so I can filter it.
So here's a bit of the code:
In ViewModel:
private string p_searchName;
public string _searchName
{
get
{
return p_searchName;
}
set
{
p_searchName = value;
base.RaisePropertyChanged("_searchName");
}
}
In xaml file
<TextBox Height="23" HorizontalAlignment="Left" Margin="2,45,0,0" Text="{Binding _searchName}"
Name="tb_SearchName" VerticalAlignment="Top" Width="99" FontFamily="Lucida Console" TabIndex="10"
Grid.Column="1" />
Any help is greatly appreciated!
Thanks
|
|
|
|
|
The binding looks right, but are there any errors in the output window when loading that form?
The other thing that it may be, is that the TextBox control doesn't update the ViewModel until it loses focus. Not sure about that though...
|
|
|
|
|
There are no errors in the output window. But you are quite right about the focus. I just added
UpdateSourceTrigger=PropertyChanged
like this
<TextBox Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"/>
and it actually works... But I really don't see why this is so.
Thanks for the help!
|
|
|
|
|
I assume the default for UpdateSourceTrigger is LostFocus, and it is set that way for performance reasons. Every time you call the setter, the setter raises the OnPropertyChanged event, WPF listens to that event and calls the getter.
|
|
|
|
|
Hmm.. thanks Jeremy!
|
|
|
|
|
Make your binding Mode="TwoWay" default is only one way so you see the data but are not telling the datacontext the data has changed
And yes the change is only notified when the user leaves the textbox
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Tried that too, didn't work unfortunately.
|
|
|
|
|
You need to set your UpdateSourceTrigger in your binding like this <TextBox Text="{Binding MyProperty, UpdateSourceTrigger=PropertyChanged}"/>
|
|
|
|
|
Yes you're right. Just did that a minute ago or so
By the way... do you know why this is so? Just curious!
|
|
|
|
|
The WPF textbox has a default UpdateSourceTrigger of LostFocus, which means that it only updates it's bound value when the user focuses on another control. By changing the trigger to update when the property changes, it updates it's bound value every time the user enters/deletes/changes a letter. Here is the MSDN page that discusses this How to: Control when the TextBox Text Updates the Source[^] .
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
|
|
|
|
|
|
Great, Glad it helped
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|
|
Every time I look at learning about DP's, I walk away confused.
Anyone know of any decent entry level tutorials?
Everything makes sense in someone's mind
|
|
|
|