Silverlight: Binding a Property to a DependencyProperty - silverlight

The XAML code:
<Canvas>
<Button x:Name="btnCanvasButton" Content="Canvas Button"
Canvas.Left="50" />
<Button x:Name="btnCanvasButton2" Content="Canvas Button 2"
Canvas.Top="25"
Width="{Binding Path=Canvas.Left, ElementName=btnCanvasButton}" />
</Canvas>
I want to bind btnCanvasButton2.Width to btnCanvasButton.Canvas.Left, but it's not working.
I also tried Path=Canvas.LeftProperty, Path=Left, Path=LeftProperty, but no luck either.
Please advise. Thx.
Peter

You need to use parentheses to bind to an attached property.
You could try:
<Button x:Name="btnCanvasButton2" Content="Canvas Button 2"
Canvas.Top="25"
Width="{Binding Path=(Canvas.Left), ElementName=btnCanvasButton}" />

Related

How to Bind the Image Source according the selected item?

How to Bind the Image Source according to the selected item from a listbox that contains items with a property Image?
<Button Name="btn">
<Button.Header>
<StackPanel>
<Image name="img">
<StackPanel>
</Button.Header>
</Button>
With the information you have provided i would do it as follows
<Button Name="btn">
<Button.Header>
<StackPanel>
<Image name="img" Source="{Binding SelectedItem.Image, ElementName=myListBox}" />
<StackPanel>
</Button.Header>
</Button>
<ListBox x:Name="myListBox">
</ListBox>
Set the Binding like this:
<Image name="img" Source="{Binding SelectedItem.Image, ElementName=myListBox}" />

Assign controls to a property in XAML

Hej all,
I wonder if it's possible to assign more than one control to a property in XAML.
Say I have 2 controls in my XAML:
<Button x:Name="_Btn1 Content="Button 1" />
<Button x:Name="_Btn2 Content="Button 2" />
<local:MyControl x:Name="_MyCtrl" Controls="{what goes here?}" />
Or should I declare my control as a container control and put all controls inside it, like so:
<local:MyControl x:Name="_MyCtrl">
<Button x:Name="_Btn1 Content="Button 1" />
<Button x:Name="_Btn2 Content="Button 2" />
</local:MyControl>
Thnx in advance!
Grtz,
Dwi
You can do that if you make MyControl inherit ItemsControl.
Then this
<local:MyControl x:Name="_MyCtrl">
<Button x:Name="_Btn1 Content="Button 1" />
<Button x:Name="_Btn2 Content="Button 2" />
</local:MyControl>
will work and this
<local:MyControl x:Name="_MyCtrl" ItemsSource="ViewModelorControlCollection" />
will work too.
The ViewModelorControlCollection can be defined in your ViewModel or as a static resource in your xaml.
It really depends what you're gonna do. Usually you'd simply define a DataTemplate instead.
<local:MyControl x:Name="_MyCtrl">
<local:MyControl.ControlsTemplate>
<DataTemplate>
<StackPanel>
<Button x:Name="btn1" Content="Button 1" />
<Button x:Name="btn2" Content="Button 2" />
</StackPanel>
</DataTemplate>
</local:MyControl.ControlsTemplate>
</local:MyControl>
You definitely need some kind of container. This can be a simple list or a specialized collection. Whatever you use in your control.
<local:MyControl>
<local:MyControl.Controls>
<ControlCollection> <!-- or whatever -->
<Button/>
<Button/>
</ControlCollection>
</local:MyControl.Controls>
</local:MyControl>
If you use a Panel or similar classes, you don't need to specify the ControlCollection explicitly. You then can define the controls like with e.g. the StackPanel.

Specifying a Button Content that has mix of text and a Binding path

How do you specify the Content of a button that is a mix of some TEXT and a Binding path?
Like this:
<Button Content= "TEXT" + "{Binding Path=ButtonContent}"
For most cases you can use StringFormat in the Bindings, like for a TextBlock
<TextBlock Text="{Binding ElementName=textBox,
Path=Text,
StringFormat='{}{0} - Added Text'}"/>
However, this has no effect on a ContentControl (which Button inherits from). Instead, you can use ContentStringFormat
<Button Content="{Binding ElementName=textBox,
Path=Text}"
ContentStringFormat="{}{0} - Added Text"/>
Also, for
ContentControl you use ContentStringFormat
HeaderedContentControl you use HeaderStringFormat
ItemsControl you use ItemStringFormat
Something like this:
<Button>
<Button.Content>
<TextBlock Text="{Binding SomeBindingPath, StringFormat='Some text {0}'}"/>
</Button.Content>
</Button>
OR
<Button>
<Button.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Some Text"/>
<TextBlock Text="{Binding SomeBindingPath}"/>
</StackPanel>
</Button.Content>
</Button>
Basically, you can put any content inside a button using the approach above.
Building on the other answers, this is a little more terse:
<Button Content="{Binding FirstName, StringFormat='Click here, {0}!'}" />

Pattern for working with a form in Silverlight 4? (How to get references to XAML elements)

I have a form:
<StackPanel Orientation="Horizontal" Visibility="{Binding Editable, Converter={StaticResource visibilityConverter}}"
ToolTipService.ToolTip="Add new topic to this group">
<sdk:AutoCompleteBox Width="160" ItemsSource="{Binding ElementName=LayoutRoot, Path=DataContext.TopicNames}" />
<Button Click="addTopicButton_Click">
<Image Source="Images/appbar.add.rest.png" />
</Button>
</StackPanel>
This form appears in a DataTemplate for an ItemsControl. I'm not sure what the best way is to get the data from the AutoCompleteBox when the button is clicked. I can't give the elements x:Name attributes, because they're in a template (right?).
How can I get around this? The Click event will give me the Button, but I need a reference to the text box. Use the Button's parent, then look through the children for the Textbox? If I factored this out into its own UserControl, I could set x:Name values, but I'd rather not do that.
Any other ideas?
Update: Here is another example of such a problem:
<ListBox x:Name="topicList"
ItemsSource="{Binding Id, Converter={StaticResource topicGroupIDConverter}}"
SelectionChanged="ListBox_SelectionChanged"
HorizontalAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}"
Width="150"
VerticalAlignment="Center"
ToolTipService.ToolTip="{Binding Description}"
ToolTipService.Placement="Right" />
<Button ToolTipService.ToolTip="Remove this topic from this group"
Visibility="{Binding ElementName=topicList,
Path=DataContext.Editable,
Converter={StaticResource visibilityConverter}}"
Click="removeTopicButton_Click"
HorizontalAlignment="Right"
Margin="10,0">
<Image Source="Images/appbar.cancel.rest.png" />
</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
When the button is clicked, I want to access topicList.DataContext. However, topicList itself is a DataTemplate in an ItemsControl, so I can't access it using its name from code-behind. How else can I do this?
You can add a property, say SelectedItemInAutoCompleteBox, to your presenter, and then can bind it to the SelectedItem property of AutoCompleteBox, using Mode=TwoWay, like this,
<sdk:AutoCompleteBox SelectedItem="{Binding Path=DataContext.SelectedItemInAutoCompleteBox, Mode=TwoWay}" ... />
You may try the same approach with Text property of AutoCompleteBox, also. See if it solves your problem.:-)
You have several choices:
If you're on Silverlight 5, use the AncestorBinding
Otherwise, use a Silverlight 4 AncestorBinding hack (it doesn't look pretty)
Or you could try DataContextProxy, which stores the DataContext in a resource so that it is accessible. Note: you should set the DataContextProxy as a Resource of topicList ListBox, not the UserControl as in Dan Wahlin's example.

Align button content

I want the content of a wpf button to be left aligned
I tried the following but the text is still centered inside the button.
<Button >
<StackPanel HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Left" Text="Save"/>
</StackPanel>
</Button>
What do i do?
Found it, it's HorizontalContentAlignment.
:)
You don't need the StackPanel or the TextBlock. All you need is
<Button HorizontalContentAlignment="Left" Content="Save" />
You can align in two way, Horizontal and Vertical
<Button Content="Export" VerticalContentAlignment="Bottom" name="MyBtn1" />
or
<Button Content="Export" HorizontalContentAlignment="Center" name="MyBtn2" />
See the follow image to view the possibilities settings:

Resources