|
Usually we use ItemSource property to set comboboxitems automatically. However, we also often hope to attach the following two options to the dropdownitems of combobox.
(None):
Sometimes we need to reset the value to none. but the itembinding combobox doesn't offer. You have to select one value.
(Add);
We also want to add a new value when the itembinding data doesn't include what we want. And we don't want to go to another page to initial this request.
I think it is a common function about combobox. but the bad thing is I can not find a good template for this.
Would anyone know about it? Thanks in advance for your help.
modified 15-Dec-11 3:01am.
|
|
|
|
|
Well, you could always do a variation of the technique I describe in my blog entry here[^].
|
|
|
|
|
The article is great . But the CompositeCollection is not included in silverlight in which my platorm is.
|
|
|
|
|
Thanks for that, but if I were you I would include vital information such as platform in future just so you don't get answers like mine.
|
|
|
|
|
I usually take a copy of the list servicing the combo and insert a couple of dummy records into the start of the list. Then you need to detect the content in the SelectedObject and deal with it when it is "Add"
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi,
I'm looking for a possibility to read the binding expression out of a DataGridTemplateColumn's CellTemplate in code behind.
My template columns look like this:
<DataGridTemplateColumn x:Name="..." Header="...">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=City}" Style="..."/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<ComboBox ... />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>
Now I want to get the value of the binding path in the datatemplate. In this case, I'like to get the value "City".
Any tips would be greatly appreciated.
Kind regards,
Nico
|
|
|
|
|
Hello,
I want to create in my project a menu like the menu in vs2008.
When you press View->OtherWindows you get a smaller sub menu
and also the options that open another menu have a small arrow next to them.
How can i do this?
Thanks.
|
|
|
|
|
MenuItem elements are hierarchical by nature, so if you were adding them through XAML, I would add them using something like the following:
<MenuItem Header="View">
<MenuItem Header="Other windows">
<MenuItem
InputGestureText="Ctrl+W, A"
Header="Command Window"/>
</MenuItem>
</MenuItem>
|
|
|
|
|
I've been a long time reader of this forum, and finally had a question I just couldn't figure out through any searches. So I signed up hoping someone would have an idea!
I am attempting to implement global hotkeys in my WPF data entry application. An example of a hotkey would be to hit F1 to save a record and move to the next record (no matter where the user has clicked, as long as it's inside the window).
Well, the functionality works correctly. When you hit F1, it saves the record. But only if you hit F1 twice. The first keypress (by observing the debugger) has an F1 KeyStates.Down value of "false". It is "true" the second time.
Here is what I've used XAML-side so far:
<Window.CommandBindings>
<CommandBinding Command="{x:Static my:MainWindow.CustomRoutedCommand}"
Executed="ExecutedCustomCommand"
CanExecute="CanExecuteCustomCommand" />
</Window.CommandBindings>
And code side:
Public Shared CustomRoutedCommand As New RoutedCommand()
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim customCommandBinding As New CommandBinding(CustomRoutedCommand, AddressOf ExecutedCustomCommand)
Dim saveKeyGesture As New KeyGesture(Key.F1)
CustomRoutedCommand.InputGestures.Add(saveKeyGesture)
End Sub
Public Sub ExecutedCustomCommand(ByVal sender As Object, ByVal e As ExecutedRoutedEventArgs)
If (Keyboard.GetKeyStates(Key.F1) = KeyStates.Down) Then
SaveRecord()
End If
End Sub
Thanks!
-- Graham
|
|
|
|
|
[Edit: fixed typo in code]
I've recently started tinkering with Silverlight and am trying to figure out how to do something that should be pretty straightforward - i.e. display a looked up value in a DataGrid column. For the purpose of simplicity, let's assume I want to display a collection of Employee records. Each Employee contains an Id (int ) and JobId (int ). The data grid should display the employee's ID and the job name (not its ID).
The only solution I can think of is to use a value converter (JobIdToJobNameConverter) that uses a static JobsProvider class that contains the mapping of job IDs to names.
<data:DataGridTextColumn
Header="Employee ID"
Binding={"Binding EmployeeId"}>
<data:DataGridTextColumn
Header="Job Name"
Binding="{Binding EmployeeJobId, Converter={StaticResource JobIdToJobNameConverter}}">
The converter looks like this:
public class JobIdToJobNameConverter : IValueConverter
{
public object Convert
(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
Job job = JobsProvider.GetJobs().FirstOrDefault
(p => p.JobId.Equals ((int) value));
return job == null ? string.Empty : job.JobName;
}
public object ConvertBack
(object value,
Type targetType,
object parameter,
CultureInfo culture)
{
return DependencyProperty.UnsetValue;
}
}
While this works, it seems there's probably a smarter solution. A gentle nudge in the right direction would be much appreciated.
Thanks,
/ravi
modified 11-Dec-11 23:48pm.
|
|
|
|
|
Ravi Bhavnani wrote: it seems there's probably a smarter solution
My first reaction to that was WTF are you doing, so I agree with you that there should be a better way.
I would look at your data source, the list of employees, that you bind to your datagrid. This probably comes from a database via a query so why not include the jobname in that original query and bind to that field.
I don't even bother binding the id fields into the datagrid as you should never actually touch the visual object when processing. You should ALWAYS use the underlying data object that it bound to the datagrid.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
My example was intentionally contrived. In doing so, I realize I left out some key information.
Assume I want to be able to display and edit the employee's job (by selecting it from an AutoCompleteBox in a GridTemplateColumn ). I'll need a JobsProvider to populate the AutoCompleteBox and was going to use the scheme I described in my original post to display the selected (and initial) job in the TextBlock in the CellTemplate . Does that sound right?
/ravi
|
|
|
|
|
Ravi Bhavnani wrote: Assume I want to be able to display and edit the employee's job
Something I almost never do in a grid, inline editing is one of the nastiest operations to code. However can you not use an edittemplate with a combobox.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
|
If you are using IE9, try running your application in "Compatibility View".
|
|
|
|
|
|
I think the browser cached that page. You need to clear cache. I know that FireFox users can press CTRL+R to reload. I do not know for chrome or IE
|
|
|
|
|
I have a silverlight application in witch the user clicks the "Record" button and his/her voice is captured to be saved later.
But when the "Record" button is pressed the following exception is thrown when it attempts to Start capturing audio.
System.Exception was unhandled by user code
Message=Class not registered (Exception from HRESULT: 0x80040154)
StackTrace:
at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
at MS.Internal.XcpImports.CaptureGraph_Start(CaptureSource Source)
at System.Windows.Media.CaptureSource.Start()
at AudioRecorder.MainPage.Record_Click(Object sender, RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, Int32 actualArgsTypeIndex, String eventName)
InnerException:
What is the solution to this problem?
|
|
|
|
|
Based on the error, something happened in the component. So reinstall the component or register the dll using REGSVR32.
|
|
|
|
|
Is there a way to make radio button groups behave as they do in Windows Forms? By that I mean when you hit tab, the focus moves to the next control following the group rather than the next radio button in the group and when you use the up and down keys, the checked state follows the selected radio button rather than having to hit space to select it. Thanks.
Eric
|
|
|
|
|
The arrow keys one is a simple fix. Just add a trigger that targets the RadioButton control. Something like:
<Style TargetType="{x:Type RadioButton}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocusWithin" Value="True">
<Trigger.Setters>
<Setter Property="IsChecked" Value="True" />
</Trigger.Setters>
</Trigger>
</Style.Triggers>
</Style>
As for the tab one... thats a little bit trickier. I don't really feel like writing code on a Saturday night, but if I was, I'd probably derive a class from RadioButton, override the key down and if its a tab, I'll search for the next control thats either not a radio button or not in the group. Keep in mind that shift-tab should work the other way . Previous control I mean. I don't think you can fix that in xaml.
|
|
|
|
|
Hi,
I have a simple grid which contains many controls.This grid is defined in resource disctionary. I wanted to hide particular grid in code. But I am not able access this grid in the code as this is defined in resource dictionary. How do I hide this grid..?
thanks..
|
|
|
|
|
You should ideally bind the visibility of the grid and control this via your view model.
Set the binding in your style, the datacontext should be the view model and you should be able to control the visibility via a property.
This[^] simple example should help you out.
|
|
|
|
|
Dear all
I'm fairly new to WPF and I think i'm trying to do something nonstandard, but maybe you can help me:
what i'm trying to do is the following:
in my code behind i have a collection (List) of a object of type Period (List<period> periods)
the object Period itself has various properties, of which some are Lists as well (Public List<string> FileReferences).
In my UI i have a datagrid, which I want to bind to these properties of one object Period in the list with Periods.
how can i dynamically bind my datagrid, two way?
thanks
|
|
|
|
|
If I get you correctly, you have an Object Period, which has a property that returns a List<FileReference> called FileReferences. Now you want to bind a DataGrid to the FileReferences property of a specific Period.
To accomplish this you would need a Property in your code behind that returns a Period object (call this SelectedPeriod). Then you can bind the DataGrid's ItemsSource property to SelectedPeriod.FileReferences.
I would just like to say, that if you have access to the models, then you should change your collections to return ObservableCollections rather than Lists. ObservableCollections provide notifications about any additions, deletions or other changes, which the bound controls can use to automatically update their display.
When I was a coder, we worked on algorithms. Today, we memorize APIs for countless libraries — those libraries have the algorithms - Eric Allman
|
|
|
|