小编Tat*_*ead的帖子

WPF XAML定义的MenuItem重用开始工作,然后消失

以下简单代码尝试在两个单独的菜单上重用Window.Resources中定义的MenuItem.

<Window x:Class="WpfApplication.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:collections="clr-namespace:System.Collections;assembly=mscorlib"
        Title="MainWindow" Height="350" Width="525">
  <Window.Resources>
    <collections:ArrayList x:Key="menuItemValues">
       <MenuItem Header="First"/>
       <MenuItem Header="Second"/>
       <MenuItem Header="Third"/>
    </collections:ArrayList>
    <MenuItem x:Key="menuItem" x:Shared="False"
              ItemsSource="{StaticResource menuItemValues}"
              Header="Shared menu item"/>
  </Window.Resources>
  <StackPanel>
    <Menu HorizontalAlignment="Left" VerticalAlignment="Top">
      <StaticResource ResourceKey="menuItem"/>
      <StaticResource ResourceKey="menuItem"/>
    </Menu>
  </StackPanel>
</Window>
Run Code Online (Sandbox Code Playgroud)

这开始很棒,当你第一次选择菜单时,一切看起来都很好.第一个菜单有所需的MenuItems,

在此输入图像描述

第二个也是如此:

在此输入图像描述

但是当您导航回第一个菜单时,MenuItems会消失:

在此输入图像描述

有人可以解释为什么菜单消失,并有办法让这个工作?

这是在调查另一个异常的SO问题时发现的.我尝试使用在另一个SO问题上讨论的策略,它似乎解决了问题,直到你第二次导航回菜单并且它消失了.

我在2台不同的机器上重现了这个问题:

  1. Win 10,VS2013 Ult V12.0.40629.00 Update 5,.NET V4.6.0138
  2. Win 7,VS2013 Prem V12.0.31101.00 Update 4,.NET V4.5.51209

wpf code-reuse xaml menuitem visual-studio-2013

8
推荐指数
1
解决办法
390
查看次数

是否可以在AWS S3中将对象到期时间设置为10分钟?

我们希望在创建对象后10分钟从S3中删除对象.目前有可能吗?

amazon-s3 amazon-web-services

8
推荐指数
1
解决办法
1517
查看次数

如何在Open xml中使用样式索引?

任何人都可以解释OpenXml中的样式索引如何工作?我有业务需求,我需要将背景颜色应用于Excel工作表中的某些单元格.并且某些样式已经应用于其他单元格.所以我需要决定我需要应用哪种样式的索引.

c# spreadsheetml openxml-sdk

8
推荐指数
1
解决办法
8546
查看次数

WPF将布尔值转换为彩色字符串

我坚持"简单"的转换.我有一个方法.它检查连接.如果建立了连接,则更改我的viewmodel的boolean属性.我想要做的是在我的表单上显示彩色字符串(红色"连接失败"或绿色"连接建立"取决于布尔属性)但我不知道如何做到这一点.

c# wpf xaml colors

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

如何使用数据绑定在WPF中设置listview项目的背景颜色?

我有一个列表视图绑定到一个可观察的类型人集合.我将以下属性添加到我的person对象:

public System.Windows.Media.SolidColorBrush Brush { get; set; }
Run Code Online (Sandbox Code Playgroud)

如何通过绑定到此属性来设置列表视图中项目的颜色?以下是我的XAML.

<Window x:Class="ObservableTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:ObservableTest"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="*"></RowDefinition>
        </Grid.RowDefinitions>

        <StackPanel Grid.Row="0" Grid.Column="0" Margin="5,5,5,5">

            <TextBlock x:Name="lblName" Text="Name"></TextBlock>
            <TextBox x:Name="txtName"></TextBox>

            <TextBlock x:Name="lblAddress" Text="Address"></TextBlock>
            <TextBox x:Name="txtAddress"></TextBox>

            <Button Grid.Column="0" Width="100" Height="20" Margin="5,5,5,5" x:Name="btnNames" Click="btnNames_Click" Content="Add"></Button>           
        </StackPanel>

        <ListView x:Name="lstNames" Margin="5,5,5,5" Grid.Column="1" Grid.Row="0">
            <ListView.View>
                <GridView x:Name="grdName">
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Address" DisplayMemberBinding="{Binding Address}"/>

                </GridView>
            </ListView.View>
        </ListView>               
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

data-binding wpf xaml colors

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

C#WPF Textblock每行不同的字体颜色

我试图使WPF文本块上的每行文本以不同的颜色显示.我有以下代码,使整个块的字体颜色为紫色,因为这是它设置的最后一种颜色.我怎样才能使每种药水以不同的颜色显示?

    private void btnShowPotions_Click(object sender, RoutedEventArgs e) {

        tbPotionInfo.Foreground = Brushes.Green;
        tbPotionInfo.Text = smallPotion.Name + "(" + smallPotion.AffectValue + ")\r\n";
        tbPotionInfo.Foreground = Brushes.Blue;
        tbPotionInfo.Text += mediumPotion.Name + "(" + mediumPotion.AffectValue + ")\r\n";
        tbPotionInfo.Foreground = Brushes.Red;
        tbPotionInfo.Text += largePotion.Name + "(" + largePotion.AffectValue + ")\r\n";
        tbPotionInfo.Foreground = Brushes.Purple;
        tbPotionInfo.Text += extremePotion.Name + "(" + extremePotion.AffectValue + ")\r\n";
    }
Run Code Online (Sandbox Code Playgroud)

c# wpf wpf-style

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

使用依赖项属性和样式触发器时,...不是DependencyProperty

在我的UserControl中:

    public ODIF.DeviceChannel Channel
    {
        get { return (ODIF.DeviceChannel)GetValue(ChannelDP); }
        set { SetValue(ChannelDP, value); }
    }
    public static readonly DependencyProperty ChannelDP = DependencyProperty.Register("ChannelProperty", typeof(ODIF.DeviceChannel), typeof(ChannelBox), new PropertyMetadata(new ODIF.DeviceChannel()));
Run Code Online (Sandbox Code Playgroud)

然后,当尝试在XAML中使用我的控件并绑定到Channel使用datatriggers时:

<local:ChannelBox VerticalAlignment="Top" HorizontalAlignment="Left" Width="200">
    <local:ChannelBox.Resources>
        <local:TypeOfConverter x:Key="TypeOfConverter"/>
    </local:ChannelBox.Resources>
    <local:ChannelBox.Style>
        <Style TargetType="{x:Type local:ChannelBox}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding mappingConnector, Converter={StaticResource TypeOfConverter}}" Value="{x:Type ODIF:MappingConnector}">
                    <Setter Property="Channel" Value="{Binding mappingConnector.plugin}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding mappingConnector, Converter={StaticResource TypeOfConverter}}" Value="{x:Type ODIF:InitialMappingConnector}">
                    <Setter Property="Channel" Value="{Binding mappingConnector.SourceChannel}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </local:ChannelBox.Style>
</local:ChannelBox>
Run Code Online (Sandbox Code Playgroud)

在XAML中引发以下错误:

属性“ Channel”不是DependencyProperty。要在标记中使用,非附加属性必须使用可访问的实例属性“ Channel”在目标类型上公开。对于附加属性,声明类型必须提供静态的“ GetChannel”和“ SetChannel”方法。 …

c# wpf xaml dependency-properties

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

WPF-使用行和列定义的网格-如何忽略某些行的列

我正在尝试在wpf应用程序中使用网格行/列定义。目前,我需要在GroupBox中实现一个列表视图。在这里,我需要忽略在视图顶部设置的列定义。

行和列的定义:

        <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
        <RowDefinition Height="260" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="250" />
        <ColumnDefinition Width="20" />
        <ColumnDefinition Width="180" />
        <ColumnDefinition Width="20" />
        <ColumnDefinition Width="180" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
Run Code Online (Sandbox Code Playgroud)

在这里,您看到我的rowDefinition高度为260。其中应该包含我的列表视图。问题在于它在我创建的列中,因此不会占用所有空间。是否有某种设置可以使该行忽略我设置的列?我仍然希望将这些列用于其他行。

在这里,您可以看到其外观的图片:

在此处输入图片说明

希望有人能帮忙,美好的一天。

wpf xaml listview groupbox grid-layout

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

在WPF应用程序C#中设置所有矩形的背景

我目前Rectangles在我的应用程序中有一套我这样定义的内容XAML;

<Rectangle Fill="LightBlue"/>
Run Code Online (Sandbox Code Playgroud)

目前我可能<10,Rectangles因此改变Coloura Rectangle不是太大的问题.

然而,随着我的应用程序的增长,我无疑会有更多,更多Rectangles,如果确定他们的颜色需要改变,我可以看到可扩展性将是一个大问题.

什么是存储的最有效的方法BackgroundRectangle,这样我可以在一个地方改变所有的改变它Rectangles在我的计划?

这也可以延伸到a的风格TextBox.如果我想为TextBox整个应用程序中的每一个设置一个自定义样式,那么可扩展的方法是什么?

c# wpf xaml scalability colors

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

清除并重新填充数据后,DataTable不会更新DataGrid。MVVM

我的viewModel是:

 public class MainWindowViewModel
 {
    public MainWindowViewModel()
    {
        PopulateDataTable();
    }
    private DataTable employeeDataTable;
    public DataTable EmployeeDataTable
    {
        get { return employeeDataTable; }
        set
        {
            employeeDataTable = value;
            OnPropertyChanged("EmployeeDataTable");
        }
    }

    private void PopulateDataTable()
    {            
        var _ds = new DataSet("Test");
        employeeDataTable = new DataTable();
        employeeDataTable = _ds.Tables.Add("DT");
        for (int i = 0; i < 10; i++)
        {
            employeeDataTable.Columns.Add("Column " + i.ToString());
        }
        for (int i = 0; i < 15; i++)
        {
            var theRow = employeeDataTable.NewRow();
            for (int j = 0; …
Run Code Online (Sandbox Code Playgroud)

c# wpf datatable datagrid mvvm

0
推荐指数
1
解决办法
2366
查看次数