|
There are plenty of blogs about Silverlight, and several CPians maintain SL blogs. Probably the premier SL site though, is Jesse Liberty's site[^]. As far as books go, I'd recommend Laurent Bugnion's excellent Silverlight 4 Unleashed[^].
|
|
|
|
|
Hi Developer,
I am using one select All Button to select all row in Datgrid (CheckBox selection).
So how to Check CheckBox in Datagrid.
Regards
Nanda.
|
|
|
|
|
In the collection that you are binding to, you have a collection of items. Each item is an instance of an object that represents a single row. Add a property to this that represents whether or not it has been checked, make it raise the PropertyChanged event and bind your Checked to this. Finally, when the user clicks select all, simply iterate over the collection and set the checked property to true. With the wonder of data binding, the UI will also update.
|
|
|
|
|
Hi,
In my application, the user has to navigate through RadioButton's group using the keyboard
By default, the arrow keys are used to navigate and the space bar used to select the radio button.
I would like the 'select' key to be another keyboard key!
How should I please process to switch the key used for the button selection by the "A" key, for instance?
Thank you for any kind help!
|
|
|
|
|
You mean add a underscore (_) to the content?
If you do "Radio Button #_1", the 1 will be underlined and a user can hit Alt+1 to select the radio button.
|
|
|
|
|
Thanks for your reply!
No, that's not what I mean (sorry english isn't my mothertongue)
I mean, being able to select the radio button which has the focus, with the key "A" instead of the default "SPACE" key!
|
|
|
|
|
A quick and dirty way would be to use your own RadioButton-derived class that responds to the key press you want, something like:
public class MyRadioButton : RadioButton
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.A)
OnClick();
else
base.OnKeyDown(e);
}
}
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Dear All,
In my application i am using WPF data grid from wpftoolkit. Here in order to achieve one of my requirement i am about to find the parent of the data grid cell.
in my grid i am having an image in an template column.
<my:DataGridTemplateColumn Width="30" >
<my:DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Image Name="imgCalendar" ToolTip="Select" Source="calendar2.jpg" Stretch="Fill" PreviewMouseUp="imgCalendar_PreviewMouseUp"></Image>
</DataTemplate>
</my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>
below is the imgCalendar_PreviewMouseUp event.
Microsoft.Windows.Controls.DataGridCell cell = sender as Microsoft.Windows.Controls.DataGridCell;
Microsoft.Windows.Controls.DataGrid dataGrid = FindVisualParent<Microsoft.Windows.Controls.DataGrid>(cell);
static T FindVisualParent<T>(UIElement element) where T : UIElement
{
UIElement parent = element;
while (parent != null)
{
T correctlyTyped = parent as T;
if (correctlyTyped != null)
return correctlyTyped;
parent = VisualTreeHelper.GetParent(parent) as UIElement;
}
return null;
}
So this is my code.
Now here when ever the grid fills , when i click the image for the first time i am not getting the parent.
But when i click once(i.e for the second time) i am getting the datagrid as parent. I don't know why its happening like this.
Please help me and thanks in advance.
|
|
|
|
|
The sender is an Image, right? So shouldn't that be something like this...
private void imgCalendar_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
Microsoft.Windows.Controls.DataGridCell cell = FindVisualParent<Microsoft.Windows.Controls.DataGridCell>(sender as Image);
Microsoft.Windows.Controls.DataGrid dataGrid = FindVisualParent<Microsoft.Windows.Controls.DataGrid>(cell);
}
If you just set the Name property on the DataGrid then you already have a DataGrid member variable of that name so I'm not sure why you need to do any of that to get a DataGrid reference...
Mark Salsbery
Microsoft MVP - Visual C++
modified on Tuesday, May 10, 2011 4:59 PM
|
|
|
|
|
Thanks Mark for your time and for reply.
I will check the method mentioned here.But the thing is same code is working on other window with the same data grid. I dont know why here.
I will try and get back to you.
|
|
|
|
|
I didn't just post a guess
I copied your code into Visual Studio and ran it in debugger and immediately saw your "sender as Microsoft.Windows.Controls.DataGridCell" return NULL.
Commented out that line and changed it to what I showed and it worked fine.
Maybe the other window that works has different code?
And why are you trying to find the DataGrid this way when you already have access to the DataGrid?
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Hi,
I'm trying desperately to get the following working, with no success :
So I hope someone here can help me.
I have a application following the MVVM pattern.
A WPF datagrid should have all it's cells backgrounds set either to Yellow or White, depending on a property on the ViewModel.
If the property returns "Locked", all cells should be Yellow.
If the property returns "Editable", all cells should be White.
Here's my XAML:
<DataGrid x:Name="grdAnsprechpartner" Style="{Binding Path=StyleDataGrid}"
AutoGenerateColumns="False"
Height="auto" Width="756"
HorizontalAlignment="Left" VerticalAlignment="Top"
ItemsSource="{Binding Path=AktuellerDatensatz.Personen}" >
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{Binding Path=CellBackground}"/>
</Style>
</DataGrid.CellStyle>
...
The Property "CellBackground" looks like this:
Public Overridable Property CellBackground() As SolidColorBrush
Get
Return p_CellBackground
End Get
Set(ByVal value As SolidColorBrush)
If value.Equals(p_CellBackground) Then Return
p_CellBackground = value
OnPropertyChanged("CellBackground")
End Set
End Property
I'm setting myViewModel.CellBackground to - for example - Brushes.Yellow.
Nothing happens.
What am I doing wrong?
If I set the CellStyle's Background-property to a StaticResource, it works.
Kind regards,
Nico
|
|
|
|
|
Are you getting any binding errors? They won't pop up as exceptions, but if you look at the Output window in Visual Studio, any data binding issues should print a line there. That's usually your best way to determine whether your binding sources are correct.
|
|
|
|
|
Hi,
thanks a lot for your answer
I checked the output and there IS an error:
System.Windows.Data Error: 40 : BindingExpression path error: 'CellBackground' property not found on 'object' ''Person' (HashCode=51777710)'. BindingExpression:Path=CellBackground; DataItem='Person' (HashCode=51777710); target element is 'DataGridCell' (Name=''); target property is 'Background' (type 'Brush')
Now I see, the datagrid's ItemsSource is bound to an ObservableCollection of "Personen".
But I don't know how to get this solved.
How do I tell the CellStyle's binding, it should look for "CellBackground" on the ViewModel?
Regards,
Nico
|
|
|
|
|
If you want to bind to properties on the ViewModel, then your grid needs to be bound to the ViewModel. Each row of the grid is bound to one item in the ItemsSource, so that's where it's looking for "CellBackground"
|
|
|
|
|
I'm not sure I understand your answer, sorry.
The datagrid's ItemsSource is bound to the ViewModel (it is bound to a ViewModel's property of type ObservableCollection).
What I did now is:
<DataGrid.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Background"
Value="{Binding Path=DataContext.CellBackground, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl, AncestorLevel=1}}"/>
</Style>
</DataGrid.CellStyle>
This works, but I'm sure that's not the most elegant solution...
|
|
|
|
|
Ahh, ok, I see where you're going with this now... I thought you wanted each cell's background to be based on the underlying data for that record, not that you wanted the entire grid to change color based on one property.
A better solution might be to have each "Person" object keep a reference to the ViewModel that owns it, like a "Parent" property. That way, you can bind to Parent.CellBackground...
Or if you're going to use FindAncestor (Which is a perfectly-legitimate method), you might want to source it to the DataGrid, not all the way up to the UserControl. That way, if you decide to move things around later, the grid stays modular.
|
|
|
|
|
Okay - perfect.
Thanks a lot!
|
|
|
|
|
What you need to do is bind the background property to the actual property that contains the Locked/Editable value, and then use a value converter to change the colour as appropriate. Here's a sample of the converter:
namespace MySample.Converters
{
using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
public class StatusTextColorConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var brush = new SolidColorBrush(Colors.White);
if (value != null && value == "Locked")
{
brush = Colors.Yellow;
}
return brush;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
} Then you declare your converter as a reference in your XAML resources:
<conv:StatusTextColorConverter x:Key="statusTextColorConverter"/> (Here conv is a link to the converter namespace). Finally, you add it to your cell grid style:
<Setter Property="Background" Value="{Binding Path=PropertyContainingEditableText, Converter={StaticResource statusTextColorConverter}}}"/> By doing this, you remove the need to implement a UI based logic item in your ViewModel.
|
|
|
|
|
Thanks a lot for your answer, i'll try this out asap.
What do you mean specially with "UI based logic item"?
|
|
|
|
|
In this case, it means that you don't have to have a property in your ViewModel that returns a colour. The converter takes care of that for you.
|
|
|
|
|
I'm trying to make a StatusBar with four text items distributed evenly across the available width like this:
-----------------------------------------------------------------
| status1 | status2 | status3 | status4 |
-----------------------------------------------------------------
Instead I'm getting this:
-----------------------------------------------------------------
| status1 | status2 | status3 | status4 |
-----------------------------------------------------------------
...when I use the following code, which looks like it should do the trick:
<Window x:Class="WpfTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Grid>
<StatusBar Grid.Row="0" Name="statusBar" VerticalAlignment="Bottom">
<StatusBarItem Content="status1" HorizontalAlignment="Stretch" />
<Separator />
<StatusBarItem Content="status2" HorizontalAlignment="Stretch" />
<Separator />
<StatusBarItem Content="status3" HorizontalAlignment="Stretch" />
<Separator />
<StatusBarItem Content="status4" HorizontalAlignment="Stretch" />
</StatusBar>
</Grid>
</Window>
What's wrong ?
Thanks,
J-L
|
|
|
|
|
I believe the StatusBar, by default, uses a DockPanel to handle its layout... Unless you specify otherwise, a DockPanel will left-dock all but the last item (Which will fill the remaining space).
You can change the layout by changing the ItemsPanel property of the StatusBar (It's just an ItemsControl):
<StatusBar ...>
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
...
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
...items...
</StatusBar>
If the separators aren't necessary, the easiest way is to use a UniformGrid, which will divide the space equally (Just set Rows = 1 and leave Columns blank). If you want to keep the separators, you could try using a regular Grid, and define columns for everything ("Auto" for separator columns, "*" for item columns).
|
|
|
|
|
It works perfectly, thanks a lot !
|
|
|
|
|
I have a Listbox of products and I bind all product names in a TextBlock inside the ListBox. For each product name I have a CheckBox next to it.
When I press a button "Check Selected products" I would like the MessageBox to list the checked products. I get an empty MessageBox and it is due to the property being null. I am not sure how to get around this problem. Any help would be appreciated.
MyView.xaml
<ListBox ItemsSource="{Binding AllProducts}" SelectionMode="Multiple">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="15"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox VerticalAlignment="Center" Grid.Column="0"
IsChecked="{Binding Path=IsSelected, RelativeSource={RelativeSource FindAncestor,AncestorType={x:Type ListBoxItem}}}" />
<TextBlock VerticalAlignment="Center" Grid.Column="1" Text="{Binding ProductName}" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
MyViewModel.cs
private string _isSelected;
public string IsSelected
{
get { return _isSelected; }
set { _isSelected = value;}
}
....
void MyCommandExecute()
{
if (this.IsSelected == null)
this.IsSelected = "property is null";
MessageBox.Show(this.IsSelected);
}
|
|
|
|