This question already has answers here:
How can I find WPF controls by name or type?
(21 answers)
Closed 2 years ago.
How does one access a control in a DataForm's EditTemplate from the code behind?
The following EditTemplate applies:
<toolkit:DataForm ItemsSource="{Binding ElementName=someDomainDataSource, Path=Data, Mode=TwoWay}">
<toolkit:DataForm.EditTemplate>
<DataTemplate>
<StackPanel>
....
<sdk:DatePicker DisplayDate="{Binding DueDate, Mode=TwoWay}}"
x:Name="dpCustomMaterialDueDate"/>
....
</StackPanel>
</DataTemplate>
</toolkit:DataForm.EditTemplate>
</toolkit:DataForm>
Is it possible to access the DatePicker from the code-behind file using the variable name dpCustomMaterialDueDate? Intellisense seems unable to find it.
Also tried to access it in the DataForm's ContentLoaded event, but no luck, i.e.
dataformPrintOrders.ContentLoaded += (sender, args) =>
{
DatePicker d = (DatePicker)
dataformPrintOrders.FindNameInContent("dpCustomMaterialDuedate");
if (d != null)
{
d.DisplayDateStart = DateTime.Now.AddMonths(-1);
d.DisplayDateEnd = DateTime.Now.AddMonths(12);
}
};
The variable d is always null.
You can also attached a Loaded event handler, and cast the sender parameter to DatePicker
Related
This question already has answers here:
WPF Binding ListBox Master/Detail
(2 answers)
Closed 4 months ago.
I have an objects, which I am receiving from an API call, I am putting them into a ListView
<ListView Background="#222222"
ScrollViewer.CanContentScroll="False"
ItemsSource="{Binding Assets}"
Style="{StaticResource ListStyle}">
</ListView>
By clicking on the item in the list, I want to get it's values (in order to call an api again on a specific object to get more info), and navigate user to the new ViewModel "details" page where I will output the api response. I can navigate a user to the new ViewModel page by creating a button with command
Command="{Binding HomeViewCommand}//as button parameter
//Actual method in ViewModel
HomeViewCommand = new RelayCommand(o =>
{
CurrentView = HomeVm;
});
But I don't know how to get the values of the item in the list user Clicked. Any help whould be nice
Bind the SelectedItem property of the ListView to a view model property:
<ListView Background="#222222"
ScrollViewer.CanContentScroll="False"
ItemsSource="{Binding Assets}"
SelectedItem="{Binding SelectedAsset}"
Style="{StaticResource ListStyle}">
</ListView>
...and get the values from this one when the command is executed:
public SelectedAsset { get; set; }
...
HomeViewCommand = new RelayCommand(o =>
{
var selectedAsset = this.SelectedAsset;
...
});
The other option would be to pass the currently selected or clicked item to the command as a command parameter using the CommandParameter property.
This question already has answers here:
Windows Phone Image Binding
(2 answers)
Closed 10 years ago.
Is it possible to bind a collection of images AND change their Canvas.Left and Canvas.Top properties? I know how to bind them to a ListBox, however, I wish to change their location and have the images scattered about.
The following code sends the data to a ListBox and renders the images one after the other in a list effect - of course.
I am unable to use Canvas.Left/Top properties as obviously the images are in a List Box.
XML
<ListBox x:Name="listBoxItems">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<Image Source="{Binding ImageUri}" Stretch="None" />
<TextBlock Text="{Binding Room.Items.ImageUri}"></TextBlock>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C#
_roomView = new RoomViewModel();
_roomView.Room = new Room
{
Items = new List<Item> {
new Item {ImageUri = "/Escape;component/Images/Items/a.jpg"},
new Item {ImageUri = "/Escape;component/Images/Items/b.png"},
new Item {ImageUri = "/Escape;component/Images/Items/b.png"}}
};
listBoxItems.ItemsSource = _roomView.Room.Items;
Does anyone know how I can achieve this?
If you use in the ItemTemplate Canvas insteed of the StackPanel you can use the AttachedProperty: Canvas.Top/Canvas.Left on the Image and on the TextBlock too. With that you can set the positions of the items as you wish. I hope it could help you.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special symbols in WPF binding - what does “{Binding Path=.}” mean?
I am not getting the meaning of following code -
<DataTemplate>
<Label>
<Hyperlink Command="{Binding Path=.}">
<TextBlock Text="{Binding Path=Header}"/>
</Hyperlink>
</Label>
</DataTemplate>
Can anyone explain it.
It is used to bind to the current source .
For more info take a look here Binding Path
hope this helps
Binding basically you are going to binding to ViewModel class. For e.g. you have an class called person and it has property called "Name". What you need to do here is, So it will bind to your Class called Person and it's property called "Name".
<TextBlock Text="{Binding Path=Name}"/>
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Components Inside Textbox/RichTextbox in DotNet
I have a textbox inside of my RichTextbox control. (The texboxt contains certain expressions that cannot otherwise be done in richtextbox itself.) I am having trouble focusing/navigating into the element when the caret is adjacent to the object. The caret is just skipping over the object. Is there any way to notice the object and focus on to it (in this case focus into the textbox)?
Actually, I should be more specific, the 'textbox' is actually a UserControl that can contain multiple texboxes and other symbols so it's not just a simple wpf 'textbox'. But essentially it behaves like a textbox.
Thanks.
<RichTextBox Name="rtb" Grid.Row="1" >
<ed:EnabledFlowDoc x:Name="efdMath" TextBlock.BaselineOffset="-10"
FontFamily="Palatino Linotype">
<Paragraph TextBlock.BaselineOffset="10" TextInput="Paragraph_TextInput_1">
Text . . .
<InlineUIContainer BaselineAlignment="Center">
<TextBox />
</InlineUIContainer>
<InlineUIContainer BaselineAlignment="Center">
<TextBox />
</InlineUIContainer>
</Paragraph>
</ed:EnabledFlowDoc>
</RichTextBox>
Also you can use IsDocumentEnabled="True" Property of RichTextBox.
The function I was looking for is this . . .
RichTextBox.CaretPosition.GetAdjacentElement(LogicalDirection Direction)
for more information, look at my newer post since i cannot delete this old post . . .
Components Inside Textbox/RichTextbox in DotNet
It can be done by override IsEnabledCore property of FlowDocument:
class EnabledFlowDoc : FlowDocument
{
protected override bool IsEnabledCore
{
get
{
return true;
}
}
}
find this solution here
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
get value of checkbox from datagrid? C#
I am trying to find a control inside the selected row in a templated DataGrid.
<DataGridTemplateColumn Header="Local">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox Name="chkImport" IsChecked="{Binding IsLocalized}"></CheckBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I am trying the following code:
var selectedRow = (DataGridRow) gridFileScan.ItemContainerGenerator.ContainerFromItem(gridFileScan.SelectedItem);
CheckBox chkImport = FindVisualChild<CheckBox>(selectedRow);
but chkImport is always null. Any ideas ??
When you debug you should be able to see the method recurse the VisualTree.
You can see the Visual Tree by using the Visual Tree Visualizer
The implementation of FindVisualChild might be flawed or the VisualTree doesn't look like what you expect.
Found it. I just had to call this method after modifying ItemsSource:
gridFileScan.UpdateLayout();