添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

Knowing Your DataContext

Every control in WPF has a data context, the control can access properties from your data context

Using Snoop you can select an object on your application and see the Data Context tab to see where your control is bound

In this example we are bound to the application view model object in C#. We can then using data binding to access the currentUserName property of the application view model

Binding – Regular Property

The RadButton property takes the below values

So we have a converter in our user control resources which converts booleans into visibile not

We then use the converter on the RadButton, which will convert our boolean (canAddToWhitelist) into one of the above values using our converter

<UserControl.Resources>
<BooleanToVisibilityConverter x:Key= "boolToVis" />
<UserControl.Resources>
<telerik:RadButton Content= "Allow" Visibility= "{Binding canAddToWhitelist, Converter={StaticResource boolToVis}}" />
<Window.Resources>
<sys:String x:Key= "strHelloWorld" >Hello, world!</sys:String>
</Window.Resources>
<StackPanel Margin= "10" >
<TextBlock Text= "{StaticResource strHelloWorld}" />
</StackPanel>
public ICommand runDataLoadCommand
get { return new DelegatingCommand(ShowConfirmRunJob); }
public void ShowConfirmRunJob()

Note: this uses the DelegatingCommand class

Command Binding With Relative Source and the 2nd Type of Ancestor in Heirarchy

The following code will create a button

<Button Name= "btnExportUnmatchedReceipts" Command= "{Binding ExportToExcelCommand}" CommandParameter= "{Binding ElementName=grvSearchList}"
DataContext= "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadTabControl},AncestorLevel=2},Path=DataContext}" />

This section

"{RelativeSource FindAncestor, AncestorType={x:Type telerik:RadTabControl},AncestorLevel=2}"

Will set the data context of the button to 2 tabcontrols above in the heirarchy

In Snoop this looks like this

So we are getting the DataContext of the top radControl  because we have said Ancestor level 2, so it will skip the SearchTabControl

The data content of the radTabControl is the ApplicationViewModel so we can access any of the properties on this data context

So looking at this binding again

<Button Name= "btnExportUnmatchedReceipts" Command= "{Binding ExportToExcelCommand}" CommandParameter= "{Binding ElementName=grvSearchList}"
DataContext= "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadTabControl},AncestorLevel=2},Path=DataContext}" />

And given that the context is the application view model, we can access the property ExportToExcelCommand

So this part of the code works because there is a property called ExportToExcelCommand to bind to

public ICommand ExportToExcelCommand
get { return new DelegatingCommand(exportUnmatchedReceipts); }
private void exportUnmatchedReceipts( object grid)
GridExporter.exportGrid(grid as RadGridView);
<Button Name= "btnExportUnmatchedReceipts" Command= "{Binding ExportToExcelCommand}" CommandParameter= "{Binding ElementName=grvSearchList}"
DataContext= "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadTabControl},AncestorLevel=2},Path=DataContext}" />
  • Create a Button
  • The data context of the button should be the same as the main tab control (application view model)
  • We say when the button is clicked it will hit the ExportToExcelCommand on the Application View Model
  • And we will pass a RadGridView as the parameter using Command Parameter
  • MSDN Data Binding

    Data Binding Overview

    Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
    To find out more, including how to control cookies, see here: Cookie Policy