我有一个ListBox,它绑定到LogMessages的ObservableCollection.
public ObservableCollection<LogMessage> LogMessages { get; set; }
public LogMessageData()
{
this.LogMessages = new ObservableCollection<LogMessage>();
}
Run Code Online (Sandbox Code Playgroud)
每条消息都有两个参数:
public class LogMessage
{
public string Msg { get; set; }
public int Severity { get; set; }
//code cut...
}
Run Code Online (Sandbox Code Playgroud)
列表框是越来越充满了这些项目,我需要的颜色代码(改变背景颜色的一个ListBoxItem)列表取决于严重性一个的LogMessage项的参数.
这是我现在在XAML中显示日志的用户控件:
<UserControl.Resources>
<AlternationConverter x:Key="BackgroundSeverityConverter">
<SolidColorBrush>Green</SolidColorBrush>
<SolidColorBrush>Yellow</SolidColorBrush>
<SolidColorBrush>Red</SolidColorBrush>
</AlternationConverter>
<Style x:Key="BindingAlternation" TargetType="{x:Type ListBoxItem}">
<Setter Property="Background"
Value="{Binding RelativeSource={RelativeSource TemplatedParent},
Path=Severity,
Converter={StaticResource BackgroundSeverityConverter}}"/>
</Style>
<DataTemplate x:Key="LogDataTemplate">
<TextBlock x:Name="logItemTextBlock" Width="Auto" Height="Auto"
Text="{Binding Msg}"/>
</DataTemplate>
</UserControl.Resources>
Run Code Online (Sandbox Code Playgroud)
和一个实际的ListBox:
<ListBox …Run Code Online (Sandbox Code Playgroud) 我想学习如何使用绑定函数.这是一个想法:我有这个参数的功能:
void print_i(int t, std::string separator)
{
std::cout << t << separator;
}
Run Code Online (Sandbox Code Playgroud)
我想这样做:
std::vector<int> elements;
// ...
for_each(elements.begin(), elements.end(), std::bind2nd(print_i, '\n'));
Run Code Online (Sandbox Code Playgroud)
但它不起作用!
这是我得到的:
/usr/include/c++/4.3/backward/binders.h: In instantiation of ‘std::binder2nd<void ()(int, std::string)>’:
main.cpp:72: instantiated from here
/usr/include/c++/4.3/backward/binders.h:138: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:141: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:145: error: ‘void ()(int, std::string)’ is not a class, struct, or union type
/usr/include/c++/4.3/backward/binders.h:149: error: ‘void ()(int, std::string)’ is …Run Code Online (Sandbox Code Playgroud) 好的,这是我的XAML:
<TextBlock Text="{Binding Path=InstanceName}"></TextBlock>
Run Code Online (Sandbox Code Playgroud)
如果InstanceName是null或空字符串,我想要Visibility="Collapsed".否则我想要Visibility="Visible".我该怎么办?
XAML:
<TextBox Name="textboxMin">
<TextBox.Text>
<Binding Path="Max">
<Binding.ValidationRules>
<local:IntValidator/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
码:
void buttonOK_Click(object sender, RoutedEventArgs e)
{
// I need to know here whether textboxMin validation is OK
// textboxMin. ???
// I need to write something like:
// if ( textboxMin.Validation.HasErrors )
// return;
}
如果至少有一个对话框控件没有通过验证 - 在XAML中,使用绑定,那么知道如何禁用OK按钮也会很好.有了这种方式,我不需要检查代码中的验证状态.
以下哪些TextBlocks的绑定会降低性能:
<Window
x:Name="Me"
x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1"
Title="MainWindow">
<StackPanel>
<TextBlock Text="{Binding Title, ElementName=Me}"/>
<TextBlock Text="{Binding Title, RelativeSource={RelativeSource AncestorType={x:Type src:MainWindow}}}"/>
</StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)
我确信当TextBlocks处于具有许多兄弟和祖先的高嵌套级别时,我的问题可能会有所不同.
(仅基于个人想法,我可能在每个特定的人中都错了!):
ElementName:
Name控件的属性应该比调用成本低GetType.Name设置时.FindAncestor:
GetType确定祖先类型; GetType比简单的Name属性获取器(可能DP不同?)的性能更高我正在尝试更新特定网站上的http绑定,我可以这样做:
Set-ItemProperty "IIS:\Sites\SiteName" -Name bindings -Value @{protocol="http";bindingInformation=*:80:hostname.site.net}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是这个命令完全取代了绑定信息,所以如果有一个https绑定,那么它会被Set-ItemProperty删除.
有没有人知道如何更新像HTTP这样的特定绑定而不必删除其他绑定或重新创建整个绑定字符串?
我非常喜欢Caliburn和命名约定绑定,并且惊讶于Visibility的绑定方式与"CanNAME"约定用于保护Action的方式不同.据我所知,只有在Caliburn中明确使用Binding时才使用BooleanToVisibilityConverter,而不是像guard方法那样自动使用.所以我想修改源代码以自动绑定到"bool?ControlNameIsVisible()"(null等于崩溃)或类似.我想知道这是否是正确的方法,如果有人已经完成了实施并且可以在这里分享它.
我在我的Model(Class X)布尔属性中:IsSelected,链接到WPF DataGrid如下:
<DataGrid SelectedIndex="{Binding SelectedXIndex,Mode=TwoWay}"
DataContext="{Binding MyViewModel}"
ItemsSource="{Binding ListX}" AutoGenerateColumns="False">
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<Setter Property="IsSelected"
Value="{Binding IsSelected, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
</Style>
</DataGrid.RowStyle>
</DataGrid>
Run Code Online (Sandbox Code Playgroud)
ListX- ObservableCollection
IsSelecte- 调用NotifyPropertyChange
它很棒.
但是当我有很多行时,我需要滚动查看它们,然后按下运行以下功能的"全选"按钮,他只选择了一些行而不是全部:(尽管所有名单上的IsSelected是真的)
public void SelectAll()
{
ListX.All(c => c.IsSelected = true);
}
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会这样?
我有文本框,文本属性绑定到viewmodel.我已将TextChanged事件绑定到viewmodel.
但问题是事件触发了每个符号都被添加到文本框中(这很好),当文本框失去焦点时,视图模型中的文本会被刷新(这对我不利).
如何在每个符号后刷新此Text属性?
或者如何将发件人作为参数发送?我认为发件人会有更新数据.
我的互动xaml:
<interactivity:Interaction.Triggers>
<interactivity:EventTrigger EventName="TextChanged">
<helpers:CustomCommandInvoker Command="{Binding UnlockChangedCommand}" />
</interactivity:EventTrigger>
</interactivity:Interaction.Triggers>
Run Code Online (Sandbox Code Playgroud)
我正在使用RelayCommand.
binding ×10
wpf ×6
mvvm ×2
c++ ×1
command ×1
datagrid ×1
elementname ×1
events ×1
iis ×1
iis-7 ×1
javafx ×1
listbox ×1
performance ×1
powershell ×1
stl ×1
string ×1
styling ×1
textblock ×1
validation ×1
visibility ×1
windows ×1
xaml ×1