本文整理汇总了C#中System.Windows.Controls.ContentControl.SetBinding方法的典型用法代码示例。如果您正苦于以下问题:C# ContentControl.SetBinding方法的具体用法?C# ContentControl.SetBinding怎么用?C# ContentControl.SetBinding使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Windows.Controls.ContentControl
的用法示例。
在下文中一共展示了ContentControl.SetBinding方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ContentAdorner
// Be sure to call the base class constructor.
public ContentAdorner(UIElement adornedElement)
: base(adornedElement)
{
this.children = new VisualCollection(this);
//
// Create the content control
//
var contentControl = new ContentControl();
//
// Bind the content control to the Adorner's ContentTemplate property, so we know what to display
//
var contentTemplateBinding = new Binding();
contentTemplateBinding.Path = new PropertyPath(AdornerContentTemplateProperty);
contentTemplateBinding.Source = adornedElement;
contentControl.SetBinding(ContentControl.ContentTemplateProperty, contentTemplateBinding);
//
// Add the ContentControl as a child
//
this.child = contentControl;
this.children.Add(this.child);
this.AddLogicalChild(this.child);
}
示例2: GenerateElement
protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
{
var binding = new Binding(((Binding)Binding).Path.Path);
binding.Source = dataItem;
var content = new ContentControl();
content.ContentTemplate = (DataTemplate)cell.FindResource(TemplateName);
content.SetBinding(ContentControl.ContentProperty, binding); return content;
}
示例3: Update
/// <summary>
/// Updates the specified binding.
/// </summary>
/// <param name="binding">The binding.</param>
/// <param name="dataContext">The data context.</param>
/// <param name="value">The value.</param>
public static void Update(this BindingBase binding, object dataContext, object value)
{
Binding b = binding as Binding;
if (b != null && b.Mode != BindingMode.TwoWay)
{
b.Mode = BindingMode.TwoWay;
}
ContentControl contentControl = new ContentControl();
contentControl.DataContext = dataContext;
contentControl.SetBinding(ContentControl.ContentProperty, b);
contentControl.Content = value;
}
示例4: Resolve
/// <summary>
/// Resolves the specified binding.
/// </summary>
/// <param name="binding">The binding.</param>
/// <param name="dataContext">The data context.</param>
/// <returns>The result of the resolved binding.</returns>
public static object Resolve(this BindingBase binding, object dataContext)
{
ContentControl contentControl = new ContentControl();
contentControl.DataContext = dataContext;
contentControl.SetBinding(ContentControl.ContentProperty, binding);
if (!string.IsNullOrEmpty(binding.StringFormat))
{
contentControl.Content = string.Format(binding.StringFormat, contentControl.Content);
}
return contentControl.Content;
}
示例5: RunTest
void RunTest ()
{
var c = new ObservableCollection<int> { 1, 2, 3, 4, 5 };
WeakControl = new ContentControl ();
WeakControl.SetBinding (ContentControl.ContentProperty, new Binding ("[0]") { Source = c });
Holder = c;
GCAndInvoke (() => {
if (WeakControl != null)
Fail ("The control should be collected");
else
Succeed ();
});
}
示例6: UIBoundToCustomerIndexer
public UIBoundToCustomerIndexer()
{
var stack = new StackPanel();
Content = stack;
var textBlock = new TextBlock();
textBlock.SetBinding(TextBlock.TextProperty, new Binding("FirstName"));
stack.Children.Add(textBlock);
var contentControl = new ContentControl();
contentControl.SetBinding(ContentControl.ContentProperty, new Binding("[0].Quantity"));
stack.Children.Add(contentControl);
contentControl = new ContentControl();
contentControl.SetBinding(ContentControl.ContentProperty, new Binding("[text].Quaity")); //purposefully misspelled
stack.Children.Add(contentControl);
}
示例7: UIBoundToCustomerWithContentControlAndTriggers
public UIBoundToCustomerWithContentControlAndTriggers()
{
var stack = new StackPanel();
Content = stack;
var textBlock = new TextBlock();
textBlock.SetBinding(TextBlock.TextProperty, new Binding("FirstName"));
stack.Children.Add(textBlock);
textBlock = new TextBlock();
textBlock.SetBinding(TextBlock.TextProperty, new Binding("LastName"));
stack.Children.Add(textBlock);
var contentControl = new ContentControl {ContentTemplate = CreateItemTemplate()};
contentControl.SetBinding(ContentControl.ContentProperty, new Binding("MailingAddress"));
stack.Children.Add(contentControl);
}
示例8: ControlWithExistingBindingOnContentWithNullValueThrows
public void ControlWithExistingBindingOnContentWithNullValueThrows()
{
var control = new ContentControl();
Binding binding = new Binding("ObjectContents");
binding.Source = new SimpleModel() { ObjectContents = null };
control.SetBinding(ContentControl.ContentProperty, binding);
IRegionAdapter adapter = new TestableContentControlRegionAdapter();
try
{
var region = (MockPresentationRegion)adapter.Initialize(control, "Region1");
Assert.Fail();
}
catch (Exception ex)
{
Assert.IsInstanceOfType(ex, typeof(InvalidOperationException));
StringAssert.Contains(ex.Message, "ContentControl's Content property is not empty.");
}
}
示例9: CreateOrUnhideDockableContent
private void CreateOrUnhideDockableContent(ContentTypes contentType, string title, string viewPropertyName, object parent)
{
if (!this.dockableContents.Keys.Contains(contentType))
{
DockableContent dockableContent = new DockableContent();
ContentControl contentControl = new ContentControl();
dockableContent.IsCloseable = true;
dockableContent.HideOnClose = false;
dockableContent.Title = title;
dockableContent.Content = contentControl;
if (parent is ResizingPanel)
{
DockablePane dockablePane = new DockablePane();
dockablePane.Items.Add(dockableContent);
ResizingPanel resizingPanel = parent as ResizingPanel;
switch (contentType)
{
case ContentTypes.PropertyInspector:
resizingPanel.Children.Add(dockablePane);
ResizingPanel.SetResizeWidth(dockablePane, new GridLength(300));
break;
case ContentTypes.Outline:
resizingPanel.Children.Insert(1, dockablePane);
ResizingPanel.SetResizeWidth(dockablePane, new GridLength(250));
break;
case ContentTypes.Toolbox:
resizingPanel.Children.Insert(0, dockablePane);
ResizingPanel.SetResizeWidth(dockablePane, new GridLength(250));
break;
}
}
else if (parent is DockablePane)
{
DockablePane dockablePane = parent as DockablePane;
dockablePane.Items.Add(dockableContent);
if (dockablePane.Parent == null)
{
this.verticalResizingPanel.Children.Add(dockablePane);
}
}
Binding dataContextBinding = new Binding(viewPropertyName);
dockableContent.SetBinding(DockableContent.DataContextProperty, dataContextBinding);
Binding contentBinding = new Binding(".");
contentControl.SetBinding(ContentControl.ContentProperty, contentBinding);
this.dockableContents[contentType] = dockableContent;
dockableContent.Closed += delegate(object sender, EventArgs args)
{
contentControl.Content = null;
this.dockableContents[contentType].DataContext = null;
this.dockableContents.Remove(contentType);
};
}
else
{
if (this.dockableContents[contentType].State == DockableContentState.Hidden)
{
this.dockableContents[contentType].Show();
}
}
}
示例10: AddTabItem
/// <summary>
/// Adds <see cref="TabItem"/> for the content object
/// </summary>
/// <param name="item">Content of the <see cref="TabItems"/></param>
private void AddTabItem(object item)
{
ContentControl contentControl = new ContentControl();
TabItem tab = new TabItem
{
DataContext = item,
Content = contentControl,
HeaderTemplate = _tabControl.ItemTemplate
};
contentControl.SetBinding(ContentControl.ContentProperty, new Binding());
tab.SetBinding(TabItem.HeaderProperty, new Binding());
_tabControl.Items.Add(tab);
}
示例11: DataContext_PreferFrameworkElementParentOverMentor
public void DataContext_PreferFrameworkElementParentOverMentor ()
{
var root1 = new Canvas { DataContext = 1 };
var root2 = new Canvas { DataContext = 2 };
var target = new ContentControl ();
target.SetBinding (FrameworkElement.DataContextProperty, new Binding ());
// This sets FrameworkElement.Parent
root1.Children.Add (target);
// This sets a new Mentor
ToolTipService.SetPlacementTarget (root2, target);
CreateAsyncTest (root1,
() => {
// We should be using the DataContext from FrameworkElement.Parent, not the mentor
Assert.AreEqual (1, target.DataContext, "#1");
// We pick up changes on the datacontext
root1.DataContext = 2;
}, () => {
Assert.AreEqual (2, target.DataContext, "#2");
// We ignore changes on the mentors datacontext
root2.DataContext = 4;
}, () => {
Assert.AreEqual (2, target.DataContext, "#3");
}
);
}
示例12: InDesignModeSettingViewModelWithGoodBindingGivesAppropriateMessage
public void InDesignModeSettingViewModelWithGoodBindingGivesAppropriateMessage()
{
Execute.InDesignMode = true;
var element = new ContentControl();
var vm = new TestViewModel();
var binding = new Binding("SubViewModel");
binding.Source = vm;
element.SetBinding(View.ModelProperty, binding);
Assert.IsInstanceOf<TextBlock>(element.Content);
var content = (TextBlock)element.Content;
Assert.AreEqual("View for TestViewModel.SubViewModel", content.Text);
}
示例13: GetSelectableValueFromItem
// Find out the value of the item using SelectedValuePath.
// If there is no SelectedValuePath then item itself is
// it's value or the innerText in case of XML node.
private object GetSelectableValueFromItem(object item, ContentControl dummyElement)
{
bool useXml = item is XmlNode;
Binding itemBinding = new Binding();
itemBinding.Source = item;
if (useXml)
{
itemBinding.XPath = SelectedValuePath;
itemBinding.Path = new PropertyPath("/InnerText");
}
else
{
itemBinding.Path = new PropertyPath(SelectedValuePath);
}
// optimize for case where there is no SelectedValuePath (meaning
// that the value of the item is the item itself, or the InnerText
// of the item)
if (string.IsNullOrEmpty(SelectedValuePath))
{
// when there's no SelectedValuePath, the binding's Path
// is either empty (CLR) or "/InnerText" (XML)
string path = itemBinding.Path.Path;
Debug.Assert(String.IsNullOrEmpty(path) || path == "/InnerText");
if (string.IsNullOrEmpty(path))
{
// CLR - item is its own selected value
return item;
}
else
{
return GetInnerText(item);
}
}
dummyElement.SetBinding(ContentControl.ContentProperty, itemBinding);
return dummyElement.Content;
}
示例14: GlyphIconButton_WithPopup
public GlyphIconButton_WithPopup()
{
Popup = new Popup()
{
AllowsTransparency = true,
};
var contentControl = new ContentControl()
{
Focusable = false,
IsTabStop = false,
};
contentControl.SetBinding(ContentProperty, new Binding() {Source = this, Path = new PropertyPath($"{nameof(PopupContent)}")});
contentControl.SetBinding(ContentTemplateProperty, new Binding() {Source = this, Path = new PropertyPath($"{nameof(PopupContentTemplate)}")});
Popup.Child = new Border
{
Child = contentControl,
Effect = new DropShadowEffect() {BlurRadius = 10, Color = Colors.Black, Opacity = 1, ShadowDepth = 0},
Margin = new Thickness(10),
};
Popup.SetBinding(Popup.IsOpenProperty, new Binding() {Source = this, Path = new PropertyPath($"{nameof(IsOpened)}")});
Popup.SetBinding(Popup.PlacementTargetProperty, new Binding() {Source = this, Path = new PropertyPath($"{nameof(PopupPlacementTarget)}")});
Popup.SetBinding(Popup.StaysOpenProperty, new Binding() {Source = this, Path = new PropertyPath($"{nameof(StaysOpen)}")});
Popup.Placement = PlacementMode.Bottom;
AddLogicalChild(Popup);
Click += GlyphIconButton_WithPopup_Click;
}
示例15: BuildGUIConfiguration
public FrameworkElement BuildGUIConfiguration()
{
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition());
grid.RowDefinitions.Add(new RowDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
grid.ColumnDefinitions.Add(new ColumnDefinition());
Label l = new Label() { Content = "Mode: " };
grid.Children.Add(l);
Grid.SetRow(l, 0);
Grid.SetColumn(l, 0);
// Combobox
ComboBox combo = new ComboBox() { VerticalAlignment = VerticalAlignment.Center };
combo.SetBinding(ComboBox.SelectedItemProperty, new Binding("SelectedMode"));
// Combo percentage mode
Grid modeGrid = new Grid();
modeGrid.ColumnDefinitions.Add(new ColumnDefinition());
modeGrid.ColumnDefinitions.Add(new ColumnDefinition());
Mode percentageMode = new Mode() { Name = "Percentage", RankMode = RankSelector.SelectionMode.Percentage };
l = new Label() { Content = "Percentage:" };
modeGrid.Children.Add(l);
Grid.SetColumn(l, 0);
TextBox tb = new TextBox() { VerticalAlignment = VerticalAlignment.Center };
tb.SetBinding(TextBox.TextProperty, new Binding("Percentage"));
modeGrid.Children.Add(tb);
Grid.SetColumn(tb, 1);
percentageMode.ModeGUI = modeGrid;
// Number mode
modeGrid = new Grid();
modeGrid.ColumnDefinitions.Add(new ColumnDefinition());
modeGrid.ColumnDefinitions.Add(new ColumnDefinition());
Mode numberMode = new Mode() { Name = "Best N", RankMode = RankSelector.SelectionMode.Number };
l = new Label() { Content = "N:" };
modeGrid.Children.Add(l);
Grid.SetColumn(l, 0);
tb = new TextBox() { VerticalAlignment = VerticalAlignment.Center };
tb.SetBinding(TextBox.TextProperty, new Binding("Number"));
modeGrid.Children.Add(tb);
Grid.SetColumn(tb, 1);
numberMode.ModeGUI = modeGrid;
combo.ItemsSource = new ObservableCollection<Mode>() { percentageMode, numberMode };
grid.Children.Add(combo);
Grid.SetRow(combo, 0);
Grid.SetColumn(combo, 1);
ContentControl c = new ContentControl();
c.SetBinding(ContentControl.ContentProperty, new Binding("SelectedMode.ModeGUI"));
grid.Children.Add(c);
Grid.SetRow(c, 1);
Grid.SetColumn(c, 0);
Grid.SetColumnSpan(c, 2);
return grid;
}