|
This is a bit long, and I'm not entirely sure how to word my question...
I am developing a prototype for a simple lookup control[^].
The source is here[^].
The Category combo is on the window, and below it is my control. When a category is selected, I want the Lookup control to get the category Id:
<ComboBox x:Name="CategoryCombo"
Grid.Row="1"
ItemsSource="{Binding Categories}"
SelectedValuePath="Id"
DisplayMemberPath="Category"
VerticalAlignment="Center"
Height="24"
Width="375"/>
<views:LookupComboControl Grid.Row="2"
CategoryId="{Binding ElementName=CategoryCombo, Path=SelectedValue, Mode=TwoWay}"
Height="70"
Width="400"/>
My Lookup control's VM is pretty simple:
public class LookupComboControlViewModel : _ModelBase
{
private List<PersonModel> _People = new List<PersonModel>();
public List<PersonModel> People
{
get { return _People; }
set
{
if (_People != value)
{
_People = value;
RaisePropertyChanged("ComboSourceData");
}
}
}
private string _CategoryName;
public string CategoryName
{
get { return _CategoryName; }
set
{
if (_CategoryName != value)
{
_CategoryName = value;
RaisePropertyChanged("CategoryName");
}
}
}
private int _CategoryId;
public int CategoryId
{
get { return _CategoryId; }
set
{
if (_CategoryId != value)
{
_CategoryId = value;
RaisePropertyChanged("CategoryId");
loadData();
}
}
}
private void loadData()
{
DAL dal = new DAL();
People = dal.GetPeopleForCategory(CategoryId);
}
}
When a category is selected, I want to set the lookup control's CateogoryId. The lookup control has this DP in it.
public LookupComboControl()
{
InitializeComponent();
this.DataContext = new LookupComboControlViewModel();
}
public int CategoryId
{
get { return (int)GetValue(CategoryIdProperty); }
set { SetValue(CategoryIdProperty, value); }
}
public static readonly DependencyProperty CategoryIdProperty =
DependencyProperty.Register("CategoryId", typeof(int), typeof(LookupComboControl));
The ultimate goal is to have the VM for the Lookup control know when the CategoryId was set. When I select a cateogory, nothing happens. What am I doing wrong here?
Thanks
If it's not broken, fix it until it is
modified 25-Mar-14 14:16pm.
|
|
|
|
|
Don't you need to bind the SelectedItem property in the Combo to something so you know what is selected?
|
|
|
|
|
I could be wrong, but I don't think so. The user control is bound to the Category combo's SelectedValue, so when a category is selected, it's value (CategoryId), should be passed to my control.
views:LookupComboControl Grid.Row="2"
CategoryId="{Binding ElementName=CategoryCombo, Path=SelectedValue, Mode=TwoWay}"
Height="70"
Width="400"/>
If it's not broken, fix it until it is
|
|
|
|
|
I think you are right in principal, but in practice I'm sure I've seen/had problems with selected value not working as expected...
For the sake of a test, change it to bind to the text property and see if it starts working- at least that narrows the issue down!
|
|
|
|
|
OK, I think you were right. The Output window was showing a Null being passed as SelectedValue.
So I made the following changes::
In the control:
public object SelectecCategory
{
get { return (object)GetValue(SelectecCategoryProperty); }
set
{
SetValue(SelectecCategoryProperty, value);
}
}
public static readonly DependencyProperty SelectecCategoryProperty =
DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl));
In the MainWindow
<ComboBox x:Name="CategoryCombo"
Grid.Row="1"
ItemsSource="{Binding Categories}"
SelectedValuePath="Id"
DisplayMemberPath="Category"
VerticalAlignment="Center"
Height="24"
Width="375"/>
<views:LookupComboControl Grid.Row="2"
SelectecCategory="{Binding ElementName=CategoryCombo, Path=SelectedItem, Mode=TwoWay}"
Height="70"
Width="400"/>
Now I get no binding errors. But the original problem still exists... Nothing happens.
So here's my question - The UserControl has the DP. Once the MainWindow sets the DP, how does the User Control's VM know about it? Again, I need to somehow get the selected category into the ViewModel.
[UPDATE]
The source is https://onedrive.live.com/redir?resid=F6FBCF1880A16630!210&authkey=!AA4CNT499dsekPY&ithint=file%2c.zip
If it's not broken, fix it until it is
|
|
|
|
|
Are you still stuck on this?
I realise it's a bit late, but anyhow...
Assuming your binding is working (didn't test your code, can't at the moment), all you would need to do is add a callback for the dependency property change.
public static readonly DependencyProperty SelectecCategoryProperty =
DependencyProperty.Register("SelectecCategory", typeof(object), typeof(LookupComboControl), new UIPropertyMetadata(new PropertyChangedCallback(OnChanged))););
...
private static void OnChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var control = obj as LookupComboControl;
control.SetCategoryOnVm();
}
private void SetCategoryOnVm()
{
vm.Category = this.SelectedCategory
}
|
|
|
|
|
I have a class called Steps and one of the properties is called Text (but it has several others as well). When a user types text in a text box (called txtText ) and click the button (called btnAddEntry ), and new Steps class is created and added to the list box.
My issue is how to format the entries in the list box. I would like the list box to show the value of the Text property of the Step just added, but I would also like the index of the item just added to be prefixed to the text.
The XAML for me list box is:
<ListBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Name="lstSteps" Margin="5">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=Text}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
So if I type the entries "First Item", "Second Item", "Third Item" into txtText and click the btnAddEntry the list box will show
First Item
Second Item
Third Item
However I want the list box to show:
1. First Item
2. Second Item
3. Third Item
I've attempted this by modifying the ListBox.ItemTemplate in various ways, but just can't seem to figure it out. I have tried things like the following:
<ListBox Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Name="lstSteps" Margin="5">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Path=Index}" />
<Label Content="{Binding Path=Text}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
As well as using RelativeSource to Self , but I can never get the list box item to prefix it's index within the list box.
I really want this down in XAML because there are buttons to move items up and down the list box after they are added (so the user can reorder items instead of deleting them and re-entering them) and if I using bindings in the item template, these numbers should auto-update.
Any help with this will is greatly appreciated. Thanks in advance for any help.
A black hole is where God tried to divide by zero.
There are 10 kinds of people in the world; those who understand binary and those who don't.
|
|
|
|
|
You did not include an ItemsSource in your XAML, so I'm hoping you just left it out (although you included other stuff)?
Please tell me you aren't manually creating ListViewItems and inserting those .
Anyways, assuming you are doing it "the right way" and using an ObservableCollection as your ItemsSource, I would write a MultiConverter that takes the ItemsSource and the item itself and returns the index of the item + 1.
Then in your XAML, you would just use StringFormat with a multi-binding to format '{0}. {1}' on a single label.
|
|
|
|
|
I am not creating ListViewItems and inserting those. I'm creating new instances of the Steps class and inserting those. The Steps class as a property called Text which is what I want displayed in the list box which is why I'm binding to (<Label Content="{Binding Path=Text}" /> ). However I wanted the index number prefixed to it, so I was hoping there was some XAML multi-binding expression to add it, without creating an ObservableCollection .
However, if I'm understanding you correctly your saying that I should binding the list box to an ObservableCollection , and when a user clicks btnAddEntry the new Steps instance is added to the ObservableCollection , and use the MultiConverter to present the data for the XAML multi-binding expression?
A black hole is where God tried to divide by zero.
There are 10 kinds of people in the world; those who understand binary and those who don't.
|
|
|
|
|
Well ListViewItem / Potato. I meant that you are inserting stuff into the Items (type ItemCollection) rather then a bound collection. Which it sounds like you are.
ItemCollection doesn't have an IndexOf method while pretty much every other list collection does.
Doesn't have to be an ObservableCollection, but since you have an add / delete / move-up / move-down. I don't know why you wouldn't use ObservableCollection. It does half the work for you.
If you are writing WPF code, it would SERIOUSLY behoove you to get into the habit of NOT operating on the controls themselves and operating on bound objects. That's what WPF was intended for and you can take advantage of a lot of features that you can't take advantage of when operating on the controls themselves.
EDIT: Almost forgot to answer your question lol...
Yes. Your label would use StringFormat on a multi-binding expression (expression 1 would be the return from your MultiValueConverter and expression 2 would be Path=Text). The MultiValueConverter would be Path=ItemsSource and {Binding} (which is the item itself). The MultiValueConverter would return ((ObservableCollection<Whatever>)values[0]).IndexOf(values[1]) + 1;
|
|
|
|
|
I need help, I created a template to personalize my sale (title bar, buttons and so on) the template is in a Diccionari Resources would like to teach me where and what are the procedures to minimize and close the window to use this template, no I ask you to make a complete program guidelines only why I upload a sample project.
File link:http://www.4shared.com/rar/r5WLt9Qpce/WindowStyle.html
|
|
|
|
|
I dont know what you want exactly, but you could take a look at this article i wrote and see if you could use what I wrote about the style. It contains information regarding Resource directory etc:
WPF-Drawing Canvas Control[^]
|
|
|
|
|
My problem is not about styles is rather like giving shares Minimize and close a window with buttons that are in the style of customizing a window.
|
|
|
|
|
It does have a custom minimize button and others, while it is not in the style, it can be placed there.
|
|
|
|
|
Pablo Roberto Cuellar Ribera wrote: what are the procedures to minimize and close the window In the Click event handler for your minimize button,
Me.WindowState = Windows.WindowState.Minimized In the Click event handler for your close button,
Me.Close()
"As beings of finite lifespan, our contributions to the sum of human knowledge is one of the greatest endeavors we can undertake and one of the defining characteristics of humanity itself"
|
|
|
|
|
I have not found a suitable answer to what I'm looking for, so answer me "Kenneth Haugland" is that if learners have checked the file I uploaded, you will see that your answer does not work. I had to solve my problem and I resolved beginner way and another drawback "Maximize and restore a sale without borders" that I will be posting later, thanks.
|
|
|
|
|
I have a datagrid and I set the ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" in file XAML. The Horizontal it works but the Vertical not works.
I tryed to drag the bottom of the window upward and the vertical scrollbar it doesn't work.
<DataGrid Name="dataGrid3" LayoutUpdated="dataGrid3_LayoutUpdated" AutoGenerateColumns="False" IsReadOnly="True" CanUserReorderColumns="False" ScrollViewer.VerticalScrollBarVisibility="Visible" ScrollViewer.HorizontalScrollBarVisibility="Visible" AlternatingRowBackground="LightGray" MouseDoubleClick="OnDoubleClick">
</DataGrid>
|
|
|
|
|
There's nothing inherently wrong there that I can see. It could be that the issue is with the container though - if the margins of the container exceed the size of the window, you could well see that it causes this problem because the grid takes it's sizing from whatever it's a child of. Use Snoop to see what the margins are right back up to the parent window.
|
|
|
|
|
Hi,
I have two stackpanel in my window and I want to remove an item from a stackpanel and add it to another stackpanel. but I want to do that with a moving animation. I dont know how many items each stackpanel has and where are their position.
is this possible ?
|
|
|
|
|
I have a style for a tab header:
<!--Center Tab Style-->
<Style x:Key="CenterTabStyle"
TargetType="{x:Type TabItem}">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
<MenuItem Header="Close All But This"
Command="{Binding CloseAllOtherTabsCommand}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate >
<Grid HorizontalAlignment="Stretch" Height="22">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="20" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="2" ></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
</Grid.RowDefinitions>
<TextBlock Grid.Row="1"
Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem} }, Path=Header}"
VerticalAlignment="Bottom" Margin="4,0,8,0"/>
<Button Grid.Row="1"
Grid.Column="1"
Height="16"
Width="16"
BorderBrush="{x:Null}"
Background="{x:Null}"
Foreground="#FF224A71"
VerticalAlignment="Center"
Padding="3,0"
Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}, Path=DataContext.CloseTabCommand}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type TabItem}}}">
<TextBlock Text="x"
VerticalAlignment="Center"
HorizontalAlignment="Center"
Margin="0,0,0,2"/>
<Button.OpacityMask>
<LinearGradientBrush EndPoint="0.5,1"
StartPoint="0.5,0">
<GradientStop Color="Black" Offset="0"/>
<GradientStop Color="#4BFFFFFF" Offset="1"/>
</LinearGradientBrush>
</Button.OpacityMask>
</Button>
</Grid>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="Background" Value="White"/>
</Style>
If I put this at the top of my view in the UserControl.Resources it works fine. If I move it to the resource file it doesn't work.
What am I doing wrong?
If it's not broken, fix it until it is
|
|
|
|
|
Have you included your ResourceDictionary in your ResourceDictionary.MergedDictionaries ? It's not enough just to have a resource dictionary, you have to tell the application about it (normally in App.xaml).
|
|
|
|
|
Yes I did
If it's not broken, fix it until it is
|
|
|
|
|
I have this checkbox column in a DataGrid
<DataGridTemplateColumn Header="Complete"
Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsCompleted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding DataContext.IsCompletedCommand,RelativeSource={RelativeSource Mode=Self}}"
CommandParameter="{Binding SelectedItem, ElementName=PhasesGrid}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
I'm trying to bind the Command to a command called IsCompletedCommand on the VM, but I can't get the syntax right.
The Output window shows:
BindingExpression path error: 'IsCompletedCommand' property not found on 'object' ''JobPhaseHeaderEntity' (HashCode=39389875)'. BindingExpression:Path=DataContext.IsCompletedCommand; DataItem='CheckBox' (Name=''); target element is 'CheckBox' (Name=''); target property is 'Command' (type 'ICommand')
The grid is bound to a list of "JobPhaseHeaderEntity". So I know that my Command binding is wrong, I just know the right syntax. Can someone help?
Thanks
[UPDATE]
I figured it out....
<DataGridTemplateColumn Header="Complete"
Width="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsCompleted, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Command="{Binding DataContext.IsCompletedCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}"
CommandParameter="{Binding SelectedItem, ElementName=PhasesGrid}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
If it's not broken, fix it until it is
modified 17-Mar-14 14:36pm.
|
|
|
|
|
I like that to occur when any text box is entered, and so I create a method in my App class that handles that :
private static void SelectAllText(object sender, RoutedEventArgs e)
{
var textBox = e.OriginalSource as TextBox;
if (textBox != null)
textBox.SelectAll();
}
and then I register a handler for the textbox GotKeyboardFocusEvent in my OnStartUP method
EventManager.RegisterClassHandler(typeof(TextBox), UIElement.GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true);
That way all textboxes select their text whenever they get focus.
Hope this helps
Everyone dies - but not everyone lives
|
|
|
|
|
Hi can you provide any source to implement row double click for datagrid using attached property.
|
|
|
|