|
You'll need to add the filters to the base query:
from c in db.Companies
join p in db.Projects on c.Id equals p.CompanyId
join j in db.Jobs on p.Id equals j.ProjectId
where c.DeletedDT == null && p.DeletedDT == null && j.DeletedDT == null
...
Also, since you're combining all of the filters using And , you don't actually need the predicate builder. Calling Where multiple times will have the same effect.
query = query.Where(i => i.ProjectStatusId == awardedId);
if (companyId > 0)
{
query = query.Where(i => i.CompanyId == companyId);
}
if (projectId > 0)
{
query = query.Where(i => i.ProjectId == projectId);
}
if (startDate != null)
{
query = query.Where(i => i.StartDate >= startDate);
}
if (endDate != null)
{
query = query.Where(i => i.StartDate <= endDate);
}
results = query.ToList();
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
Here's my link:
<TextBlock Grid.Row="0"
Margin="5">
<Hyperlink x:Name="link">
<TextBlock Text="Purchasing"
FontSize="14"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ActionCommand, RelativeSource={RelativeSource AncestorType=Window}}"
CommandParameter="{Binding ElementName=link}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Hyperlink>
</TextBlock>
I get
System.Windows.Data Error: 40 : BindingExpression path error: 'ActionCommand' property not found on 'object' ''MainWindowView' (Name='window')'. BindingExpression:Path=ActionCommand; DataItem='MainWindowView' (Name='window'); target element is 'InvokeCommandAction' (HashCode=36783718); target property is 'Command' (type 'ICommand')
It is trying to bind to the VIEW, not the VIEWMODEL. What's wrong here?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Your RelativeSource is set a Window. As WPF allows you to bind to other controls, as well as ViewModels, it makes no assumptions about what you are trying to do so it doesn't automatically try to pick up the DataContext. Try changing this to {Binding DataContext.ActionCommand, RelativeSource={RelativeSource AncestorType=Window}} instead.
This space for rent
|
|
|
|
|
That did it. Thank you.
Any thoughts on this? Probably similar but I can't seem to get it right.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I'm adding a context menu to my tabs with "Close", "Close All", and "Close All But This". Heres' the XAML:
<Style TargetType="TabItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu>
<MenuItem Header="Close All"
Command="{Binding DataContext.CloseTabCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}"
CommandParameter="{Binding }"/>
<MenuItem Header="Close All"
Command="{Binding DataContext.CloseAllTabsCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}"
CommandParameter="{Binding }"/>
<MenuItem Header="Close All But This"
Command="{Binding DataContext.CloseAllButThisTabCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type MenuItem}, AncestorLevel=1}}"
CommandParameter="{Binding }"/>
</ContextMenu>
</Setter.Value>
</Setter>
</Style>
The biding isn't work. The commands aren't called. Here's the output window messages:
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.MenuItem', AncestorLevel='1''. BindingExpression:Path=DataContext.CloseTabCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.MenuItem', AncestorLevel='1''. BindingExpression:Path=DataContext.CloseAllTabsCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.MenuItem', AncestorLevel='1''. BindingExpression:Path=DataContext.CloseAllButThisTabCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
What's the right way to bind the menu item's command to the main window's VM?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
Well that didn't work. I've tried a number of things. This is what I have now.
<Style TargetType="TabItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding DataContext, ElementName=Window}">
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding}"/>
<MenuItem Header="Close All Tabs"
Command="{Binding CloseAllTabsCommand}"
CommandParameter="{Binding}"/>
<MenuItem Header="Close All But This"
Command="{Binding CloseAllButThisTabCommand}"
CommandParameter="{Binding}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid Name="Panel">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="LightSkyBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Panel" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
ElementName is looking for an element named Window. If you haven't named your window "Window", that's not going to work. The RelativeSource you do in your other post should find the owning Window.
This space for rent
|
|
|
|
|
I have the window's name set to 'window'.
This is driving me nuts. I can't seem to get this right. here's what I have now:
<Style TargetType="TabItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}"
x:Name="tabMenu">
<MenuItem Header="Close"
Command="{Binding CloseTabCommand}"
CommandParameter="{Binding}"/>
<MenuItem Header="Close All Tabs"
Command="{Binding CloseAllTabsCommand}"
CommandParameter="{Binding}"/>
<MenuItem Header="Close All But This"
Command="{Binding CloseAllButThisTabCommand}"
CommandParameter="{Binding}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid Name="Panel">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="LightSkyBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Panel" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
The Output Window shows
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'ContextMenu' (Name='tabMenu'); target property is 'DataContext' (type 'Object')
Again, it looks like it can't find the Window.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Sorry, I misread the part that this was a context menu. A context menu doesn't sit in the visual tree so you can't relative source back to it. What you're looking to do will be some variation of this[^].
This space for rent
|
|
|
|
|
Looks like the button's Tag property is being set to the window's DataContext, and the context menu is getting its DataContext off the button's tag.
Interesting idea. Not sure how to apply this in my style.
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Following up on this.
I found a solution here.
Now my commands are firing. But the CommandParameters are all null.
Here's my XAML now:
<Style TargetType="TabItem">
<Setter Property="ContextMenu">
<Setter.Value>
<ContextMenu DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" >
<MenuItem Header="Class Tab"
Command="{Binding Source={StaticResource Proxy}, Path=Data.CloseTabCommand}"
CommandParameter="{Binding}"/>
<MenuItem Header="Class All Tabs"
Command="{Binding Source={StaticResource Proxy}, Path=Data.CloseAllTabsCommand}"
CommandParameter="{Binding}"/>
<MenuItem Header="Close All But This"
Command="{Binding Source={StaticResource Proxy}, Path=Data.CloseAllButThisTabCommand}"
CommandParameter="{Binding}"/>
</ContextMenu>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid Name="Panel">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="10,2"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Panel" Property="Background" Value="LightSkyBlue" />
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Panel" Property="Background" Value="White" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I tried using element name but still no luck. Any thoughts?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
|
I'm trying to create a listbox of hyperlinks. Here's the XAML:
<ListBox ItemsSource="{Binding QuickActions}"
x:Name="quickItemList">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Grid.Row="0"
Margin="5">
<Hyperlink x:Name="link">
<TextBlock Text="{Binding Caption}"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ElementName=uc, Path=DataContext.QuickActionCommand}"
CommandParameter="{Binding ElementName=quickItemList, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Hyperlink>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
and the VM code:
private ICommand _QuickActionCommand;
public ICommand QuickActionCommand
{
get
{
if (_QuickActionCommand == null)
_QuickActionCommand = new RelayCommand<object>(p => QuickActionExecuted(p), p => QuickActionCanExecute());
return _QuickActionCommand;
}
}
private bool QuickActionCanExecute()
{
return true;
}
private void QuickActionExecuted(QuickActionModel model)
{
}
When I click the link the parameter 'model' is null. The SelectedItem in the list is null. I know there's a way in the XAML to code this but I just can't get it right.
Anyone know how to do this?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Caveat - this is a guess
You are passing the list in the commandparameter can you pass the selected item instead.
However I would build it differently. Bind the selected item to the VM and the textbox click event uses the selected item to get the link, no need to pass parameters from the view in the click event.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Try:
CommandParameter="{Binding}"
Your command also looks a bit odd - how does a RelayCommand<object> convert the parameter from object to QuickActionModel ?
If it still doesn't work, check the output window in Visual Studio to see if there are any binding errors.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
|
I'm creating a control in WPF that looks like this
Right now I'm designing the Bay. It's really just 3 simple nested borders.
What I want is when the mouse is over ANY part of the control (any of the 3 borders), then show the red border.
The mouseOeverBorderStyle's Setters don't like the TargetName. This has to be fairly easy. Anyone know how to do this?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
Unfortunately, the IsMouseOver property doesn't work unless you have a background. However, you can set the background to "Transparent":
<Style x:Key="mouseOverBorderStyle" TargetType="{x:Type Border}" BasedOn="{StaticResource borderStyle}">
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="Background" Value="Transparent"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="BorderBrush" Value="Red"/>
</Trigger>
</Style.Triggers>
</Style>
You'll want to set the border thickness outside of the trigger; otherwise, the inner border shrinks slightly when you mouse over the control.
There's still a 2px white border around the edge which won't trigger the mouse-over behaviour. But I'm sure most people won't notice it.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Worked great. Thanks!
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I have a problem with a DataGrid consuming a huge amount of memory when we have ~15,000 cells, each cell holding an edit control ie CheckBox, TextBox or ComboBox. The view takes 40 seconds to appear, and then consumes 1GB RAM. I can turn on virtualisation of rows and columns, which cures the slow start, and reduces memory by a factor of 3. However, that makes scrolling unacceptably slow ie many seconds.
I have a test app which allows me to test methods to improve performance. It displays values in a 2D matric, which is defined as a list of Row instance. Each Row object contains an array of node instances which hold the values.
The DataGrid is simple enough:
<DataGrid Grid.Row="2" Name="_dataGrid2"
ItemsSource="{Binding Matrix}"
AlternationCount="2"
AutoGenerateColumns="False"
Style="{StaticResource styleDataGridNoSelection}"
EnableRowVirtualization="True"
EnableColumnVirtualization="True"
VirtualizingStackPanel.IsVirtualizing="False"
VirtualizingStackPanel.VirtualizationMode="Recycling"
ScrollViewer.CanContentScroll="True"
HeadersVisibility="None"/>
The view constructor creates the column data templates:
public MainWindow()
{
InitializeComponent();
MainViewModel viewModel = new MainViewModel();
DataContext = viewModel;
_gridDataTemplateSelector = new GridDataTemplateSelector();
_gridDataTemplateSelector.LabelDataTemplate = Resources["LabelDataTemplate"] as DataTemplate;
_gridDataTemplateSelector.TextBoxDataTemplate = Resources["TextBoxDataTemplate"] as DataTemplate;
_gridDataTemplateSelector.CheckBoxDataTemplate = Resources["CheckBoxDataTemplate"] as DataTemplate;
_gridDataTemplateSelector.ComboBoxDataTemplate = Resources["ComboBoxDataTemplate"] as DataTemplate;
for (int i = 0; i < Row.constColumnCount; i++)
{
DataGridTemplateColumn col = new DataGridTemplateColumn();
FrameworkElementFactory fef = new FrameworkElementFactory(typeof(ContentPresenter));
Binding binding = new Binding();
fef.SetBinding(ContentPresenter.ContentProperty, binding);
fef.SetValue(ContentPresenter.ContentTemplateSelectorProperty, _gridDataTemplateSelector);
binding.Path = new PropertyPath("Values[" + i + "]");
DataTemplate dataTemplate = new DataTemplate();
dataTemplate.VisualTree = fef;
col.CellTemplate = dataTemplate;
_dataGrid2.Columns.Add(col);
}
The view model is also simple:
public class MainViewModel : System.ComponentModel.INotifyPropertyChanged
{
public MainViewModel()
{
const int RowCount = 150;
_matrix = new Row[RowCount];
for (int i = 0; i < RowCount; i++)
{
_matrix[i] = new Row("Row" + i.ToString(), i);
}
}
private Row[] _matrix;
public Row[] Matrix
{
get
{
return _matrix;
}
}
#region INotifyPropertyChanged
public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
public virtual void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
}
}
#endregion
}
And the row is just a simple array of nodes:
public class Row
{
private string name;
private Model.INode[] _values;
public string Name { get { return name; } }
public Model.INode[] Values { get { return _values; } }
public int Index { get; set; }
public const int constColumnCount = 90;
public Row(string name, int index)
{
Index = index;
this.name = name;
_values = new Model.INode[constColumnCount];
for (int i = 0; i < constColumnCount; i++)
{
if (index == 0)
{
_values[i] = new Model.LabelNode();
(_values[i] as Model.LabelNode).Value = i.ToString();
}
else
{
if (i == 0)
{
_values[i] = new Model.LabelNode();
(_values[i] as Model.LabelNode).Value = index.ToString();
}
else
{
switch ((i + index) % 3)
{
case 0:
_values[i] = new Model.TextNode();
(_values[i] as Model.TextNode).Value = i.ToString();
break;
case 1:
_values[i] = new Model.CheckBoxNode();
(_values[i] as Model.CheckBoxNode).Value = true;
break;
case 2:
_values[i] = new Model.ComboBoxNode();
(_values[i] as Model.ComboBoxNode).Values = new List<string>(2) { (i + index).ToString(), (i + index + 1).ToString() };
(_values[i] as Model.ComboBoxNode).Value = (_values[i] as Model.ComboBoxNode).Values[0];
break;
}
}
}
}
} }
The node classes are basic shells eg:
class TextNode : INode
{
public string Value { get; set; }
}
|
|
|
|
|
For your DataGrid, set this
ScrollViewer.IsDeferredScrollingEnabled="True"
This space for rent
|
|
|
|
|
Thanks. Sadly that doesn't really improve things, still slow.
|
|
|
|