Binding a control's x:Name - c#

When I add
<TextBlock Text="{Binding SettingName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
Everuthing is ok. But when
<TextBlock x:Name="{Binding SettingTextBlockName}" Text="{Binding SettingName}" TextWrapping="Wrap" Margin="10,-2,10,0" Style="{StaticResource PhoneTextSubtleStyle}" />
constructor are breaking.
But I need different names in all elements.

x:Name is a special property. As a matter of fact it's not a property at all, it's an attribute that maps the name or id property of the element to x:Name. Binding only works when applied to a DependencyProperty, so it cannot work on x:Name. It must be set manually.
If you want to distinguish between objects in runtime, you can set the Tag attribute, which tolerates everything.
more on x:Name: http://msdn.microsoft.com/en-us/library/ms752290.aspx

You should use FrameworkElement.Tag property, according to MSDN
FrameworkElement.Tag gets or sets an arbitrary object value that can
be used to store custom information about this element.
What use is the Tag property in .net

Related

Custom control binding is not updating

I'm having trouble getting my binding to work correctly. Basically I have this, call this Control1.xaml. The commented out portion of the code binds correctly and updates as expected.
<progControls:CalibrationSummary
</progControls:CalibrationSummary>
<!--<TextBlock Text="{Binding Path=NumberOfCalibrations, Mode=OneWay}"/>-->
However, if I put that commented code in a custom control called CalibrationsSummary.xaml, I cannot bind this to NumberOfCalibrations.
Here's what CalibrationsSummary looks like
<Grid>
<TextBlock Text="{Binding Path=NumberOfCalibrations, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}"/>
</Grid>
Note that I do use RelativeSource to try to get the property associated with Control1.xaml, tried TemplateBinding also. What am I doing wrong?
CalibrationSummary has no TemplatedParent unless you have put it in a ControlTemplate.
If you don't explicitly set the DataContext of the property of CalibrationSummary somewhere, it will inherit the DataContext from its parent control (which I assume is Control1) and then you can bind any property of this control's DataContext as usual without specifying any source:
<Grid>
<TextBlock Text="{Binding Path=NumberOfCalibrations}"/>
</Grid>

XAML Set a Resource Key by Binding

I want to Change the Style of a Grid dynamically. For that Purpose let's suppose I have 3 Textblocks with 3 Contents defined.
<TextBlock x:Name="Block1" Text="key1" />
<TextBlock x:Name="Block2" Text="key2" />
<TextBlock x:Name="Block3" Text="key3" />
For each of the keys (1,2,3) there is a Style defined with the Name x:Key="key1".
Now i want something like this in my Grid:
<Grid Style="{DynamicResource {Binding ElementName=Block1, Path=Text}} />
Is this possible in Default XAML or do I have to find a Workaround?
Is this possible in Default XAML or do I have to find a Workaround?
No, I am afraid you cannot use the StaticResource or DynamicResource markup extension with "dynamic" values like this. The keys of the resources must be known at compile time.
Binding to a property and use a converter would be one way:
<Grid Style="{Binding ElementName=Block1, Path=Text, Converter={StaticResource converter}}">
But I guess that's a workaround.

Textbox tag and IsHitTestVisible property

<TextBox Foreground="Black"
FontFamily="Times New Roman"
FontWeight="Bold"
FontSize="15"
MaxHeight="50"
Margin="6,95,40.067,0"
Name="txt1" VerticalAlignment="Top"
IsHitTestVisible="False"
Height="30"
Grid.Row="4"
Grid.Column="2"/>
What is the role of IsHitTestVisible property on TextBox?
When you have a control inside another control, like, If you have a TextBox inside... lets say, another TextBox. Then by setting the isHitTestvisible property of the parent control to False you allow the user to type in the child TextBox. If you set it to True then the RoutedEvent will be handled at the parent control level.
This property is mostly used when you work with Adorners.
Check this posts
http://social.msdn.microsoft.com/Forums/en/wpf/thread/7c352827-b4ed-493c-8a68-58179ad801fc
http://msdn.microsoft.com/en-us/library/system.windows.uielement.ishittestvisible.aspx
true if this element could be returned as a hit test result from at least one point; otherwise, false. The default value is true.
Source: MSDN
See also: Hit Testing in the Visual Layer

Silverlight Element Binding

I have a custom component, ExportCommandButton, that has two attached properties. This component is designed to be attached to a button. When clicked, the component would initiate the export method of the grid (Telerik RadGridView). The problem I have is how can I pass the grid to the component via one of the attached properties? I've tried element to element binding, but the GridView set property never fires. How do you bind to a control and not a property of the control?
<Button IsEnabled="{Binding Loaded}"
cmd:ExportCommandButton.GridView="{Binding ElementName=MyGrid}"
cmd:ExportCommandButton.Converter="{StaticResource MyConverter}">
<Button.Content>
<StackPanel Orientation="Horizontal">
<Image Source="/Assets/xls.png" />
<TextBlock VerticalAlignment="Center" Text="Export" Margin="5,0,0,0" />
</StackPanel>
</Button.Content>
</Button>
Your syntax seems right. The CLR property setter is not called because the binding directly updates the dependency property, without passing by the property which is here for convenience. Use the propertyChangedCallback parameter of your attached property metadata to listen for changes.

WPF: Cannot set properties on property elements weirdness

private TextBlock _caption = new TextBlock();
public TextBlock Caption
{
get { return _caption; }
set { _caption = value; }
}
<l:CustomPanel>
<l:CustomPanel.Caption Text="Caption text" FontSize="18" Foreground="White" />
</l:CustomPanel>
Gives me the following error:
Cannot set properties on property elements.
If I use:
<l:CustomPanel>
<l:CustomPanel.Caption>
<TextBlock Text="Caption text" FontSize="18" Foreground="White" />
</l:CustomPanel.Caption>
</l:CustomPanel>
My TextBlock shows up fine but it's nested inside another TextBlock like so, it even seems to add itself outside of the Caption property:
<l:CustomPanel>
<l:CustomPanel.Caption>
<TextBlock>
<InlineUIContainer>
<TextBlock Text="Caption text" FontSize="18" Foreground="White" />
</InlineUIContainer>
</TextBlock>
</l:CustomPanel.Caption>
<TextBlock>
<InlineUIContainer>
<TextBlock Text="Caption text" FontSize="18" Foreground="White" />
</InlineUIContainer>
</TextBlock>
</l:CustomPanel>
As you might have already guessed, what i'd like my code to do is to set my Caption property from XAML on a custom panel, if this is possible.
I've also tried the same code with a DependencyProperty to no avail.
So, anyone that can help me with this problem?
I can explain what is going wrong and how to fix it.
First,
<l:CustomPanel>
<l:CustomPanel.Caption Text="Caption text" FontSize="18" Foreground="White" />
is a simple syntax error. The <l:CustomPanel.Caption> syntax does not accept XML attributes - the property value must be within the element.
This is proper property element syntax:
<l:CustomPanel>
<l:CustomPanel.Caption>
<TextBlock Text="Caption text" FontSize="18" Foreground="White" />
</l:CustomPanel.Caption>
</l:CustomPanel>
but:
Property element syntax works only with DependencyProperties (so it didn't work with your CLR property) and
Property element syntax always honors the ContentPropertyAttribute of the property type
Since TextBlock has a [ContentPropertyAttribute("Inlines")], the property element syntax is trying to add the TextBlock to the Inlines collection.
The solution is simple: Declare your property as a DependencyProperty of type UIElement instead of type TextBlock. This has the additional advantage of not restricting the display of content to just a TextBlock. If you really do want to restrict it to just a TextBlock, you can use a validation callback.
public UIElement Content { get { ...
public static readonly DependencyProperty ContentProperty = ...
Just got a non-ideal workaround from a colleague of mine. It involves declaring the Caption property as a resource like:
<Page.Resources>
<TextBlock x:Key="test" Text="Caption text" FontSize="18" Foreground="White" />
</Page.Resources>
<l:CustomPanel Caption="{StaticResource test}" />
I'd still like to know why I can't use the two previous options, so if anyone knows please answer. :)
It seems that you can get this error (in Silverlight 4 and 5 at least) if you specify a namespace on the element. For example:
<Path>
<MapLayer.Position xmlns="clr-namespace:Microsoft.Maps.MapControl">
...
In this case MapLayer.Position is an attached property. It seems that the Silverlight parser requires the namespace to be defined using a prefix:
<Path xmlns:map="clr-namespace:Microsoft.Maps.MapControl">
<map:MapLayer.Position>
...

Categories

Resources