Monthly Archives: March 2015

WPF Databind to read-only DataGrid.SelectedItems property

Thanks a million to the reference below:

I have spent one day looking for a MVVM solution to bind to DataGrid.SelectedItems property. WPF does not allow this because DataGrid.SelectedItems is read-only. However, I need to get hold of all selected items when performing “delete” action, or just to decide if a right-click menu item should appear or not, and I CAN’T!!!

After a day’s Googling and trying out millions of not-straightforward-and-ultimately-not-working ways, I have found the article above, which consists of 3 simple classes that just work! I would really like to share this with others who are having this trouble.

WPF DataGrid tricks

  • Edit in upper case for a text column:
    <DataGridTextColumn Header="Fund" Binding="{Binding Fund}">
         <DataGridTextColumn.EditingElementStyle>
             <Style TargetType="TextBox">
                 <Setter Property="CharacterCasing" Value="Upper"/>
             </Style>
         </DataGridTextColumn.EditingElementStyle>
    </DataGridTextColumn>              
    
  • String.Empty in an array
    <x:Array>
     <system:String>string1</system:String>
     <x:Static Member="system:String.Empty" />
     <system:String>string2</system:String>
    </x:Array>
  • DataGridComboBoxColumn bindings
     <DataGridComboBoxColumn Header="FundShare"
     SelectedItemBinding="{Binding FundShare}"
     DisplayMemberPath="FundShareName"
     SortMemberPath="FundShare.FundShareName"
     ClipboardContentBinding="{Binding FundShare.FundShareName}"
     ItemsSource="{Binding Source={x:Static viewmodel:IFSFundMappingViewModel.FundShares}}">

References:

Mass copy tasks from one server to another (Windows Server 2008)

Ref:

Pointers:

  • All the Tasks created in Windows are stored in the Task Folder of Windows System Directory which in my case is C:\Windows\System32\Tasks.
  • Copy the task files to the machine you want to export to.
  • Rename the files with extension as .XML. I have verified that this XML file is exactly the same as the XML file generated via Export function.
  • The script below exports all jobs in a task folder. %%~nf extracts the file name of a file, which is the same as the task name. Note that there cannot be ‘.’ present in the task name as otherwise the content after ‘.’ is treated as extension.
    for %%f in (C:\Windows\System32\Tasks\{task folder}\*) do (
    copy /y "%%f" "{export folder}\%%~nf.xml"
    )
  • The script below creates the jobs exported in the above step. Note that -F means it will override any existing task with the same name. /RU,/RP is used instead of /S /U /P because I cannot figure out what /S should be for local user…
    for %%f in ("{export folder}\*.xml") do (
    schtasks /create /xml "%%f" /tn "{task folder}\%%~nf" /RU "{domain\user_id}" /RP {pwd} /F
    )

Reset Identity Seed

DBCC CHECKIDENT ('[TestTable]', RESEED, 0)

from StackOverflow

This is useful for resetting the identity column if you ever make a mistake by inserting duplicated rows and end up deleting one of them. My use-case was that this identity column needs to be in-sync with the other databases and it is used as foreign keys in some other tables.

Alternatively, you can turn it off:

SET IDENTITY_INSERT sometableWithIdentity ON

INSERT sometableWithIdentity (IdentityColumn, col2, col3, ...)
VALUES (AnIdentityValue, col2value, col3value, ...)

SET IDENTITY_INSERT sometableWithIdentity OFF