标签: binding

WPF,XAML:如何使用ListBox ItemsSource对象的属性绑定样式ListBoxItem?

我有一个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)

wpf xaml binding listbox styling

16
推荐指数
1
解决办法
4万
查看次数

如何使用bind1st和bind2nd?

我想学习如何使用绑定函数.这是一个想法:我有这个参数的功能:

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)

c++ binding stl

16
推荐指数
2
解决办法
2万
查看次数

WPF:通过字符串内容绑定可见性

好的,这是我的XAML:

<TextBlock Text="{Binding Path=InstanceName}"></TextBlock>
Run Code Online (Sandbox Code Playgroud)

如果InstanceName是null或空字符串,我想要Visibility="Collapsed".否则我想要Visibility="Visible".我该怎么办?

wpf binding visibility textblock

16
推荐指数
1
解决办法
2万
查看次数

从代码访问WPF控件验证规则

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按钮也会很好.有了这种方式,我不需要检查代码中的验证状态.

validation wpf binding

16
推荐指数
2
解决办法
2万
查看次数

ElementName与RelativeResource?

以下哪些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:

    • 可能会搜索并比较当前元素与更多控制,通过它的所有孩子,兄弟姐妹,叔叔和包括祖先在内的大叔(可能有所有注册名称的HashTable?)
    • 获取Name控件的属性应该比调用成本低GetType.
    • 比较字符串比比较类型便宜,特别是当您知道大多数控件甚至没有它们的Name设置时.
  • FindAncestor:

    • 只会通过祖先,而不是兄弟姐妹的"叔叔","表兄弟"等进行迭代.
    • 最有可能用于GetType确定祖先类型; GetType比简单的Name属性获取器(可能DP不同?)的性能更高

wpf performance binding relativesource elementname

16
推荐指数
1
解决办法
6237
查看次数

Powershell更新IIS绑定

我正在尝试更新特定网站上的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这样的特定绑定而不必删除其他绑定或重新创建整个绑定字符串?

windows iis powershell binding iis-7

16
推荐指数
3
解决办法
2万
查看次数

可见性使用命名约定自动绑定

我非常喜欢Caliburn和命名约定绑定,并且惊讶于Visibility的绑定方式与"CanNAME"约定用于保护Action的方式不同.据我所知,只有在Caliburn中明确使用Binding时才使用BooleanToVisibilityConverter,而不是像guard方法那样自动使用.所以我想修改源代码以自动绑定到"bool?ControlNameIsVisible()"(null等于崩溃)或类似.我想知道这是否是正确的方法,如果有人已经完成了实施并且可以在这里分享它.

binding naming-conventions caliburn.micro

16
推荐指数
2
解决办法
3863
查看次数

在WPF DataGrid中选择绑定

我在我的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)

我不明白为什么会这样?

wpf binding datagrid mvvm

16
推荐指数
1
解决办法
9265
查看次数

WPF MVVM文本框文本绑定vs changedText事件

我有文本框,文本属性绑定到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.

wpf events binding command mvvm

16
推荐指数
1
解决办法
2万
查看次数

何时在String上使用StringProperty?

我正在JavaFX中开发一个应用程序,我希望在单个Person类中表示Person的信息.我遇到了一个教程,其中一个人的名字被表示为StringProperty代替String.我已经搜索了这些的不同之处并发现了这个和这个但是解释并不足以让我抓住这个概念.网上的一些帖子表示使用StringProperty结束有优势String但却无法提及.

现在的问题是:什么样的条件需要一个使用StringProperty过String,什么是这样做的优势是什么?

为什么这个:

StringProperty firstName;

在此:

String firstName;

string binding javafx

16
推荐指数
2
解决办法
2万
查看次数