Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xaml 将属性绑定到ResourceDictionary中定义的样式,以消除代码重复_Xaml_Resourcedictionary - Fatal编程技术网

Xaml 将属性绑定到ResourceDictionary中定义的样式,以消除代码重复

Xaml 将属性绑定到ResourceDictionary中定义的样式,以消除代码重复,xaml,resourcedictionary,Xaml,Resourcedictionary,我有以下XAML代码: <Window x:Class="LinkButton.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/b

我有以下XAML代码:

<Window x:Class="LinkButton.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:LinkButton"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        DataContext="{StaticResource MainWindowVM}">

    <Window.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Margin" Value="10" />
        </Style>
    </Window.Resources>

    <Grid>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

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

        <TextBlock Grid.Row="0" Grid.Column="0" Text="ddk" />
        <TextBlock Grid.Row="0" Grid.Column="1" >
             <Hyperlink Command="{Binding Link}"  
                       CommandParameter="{Binding}"
                       Foreground="Blue" >
               <Hyperlink.Inlines>
                   <TextBlock>
                        <TextBlock.Style>
                            <Style>
                                <Setter Property="TextBlock.Text" Value="{Binding Description01.Header}" />
                            </Style>
                        </TextBlock.Style>
                   </TextBlock>
               </Hyperlink.Inlines>
                </Hyperlink>
        </TextBlock>
        <TextBlock Grid.Row="1" Grid.Column="0" Text="dde" />
        <TextBlock Grid.Row="1" Grid.Column="1">
            <Hyperlink Command="{Binding Link}"  
                       CommandParameter="{Binding}"
                       Foreground="Blue" >
               <Hyperlink.Inlines>
                   <TextBlock>
                        <TextBlock.Style>
                            <Style>
                                <Setter Property="TextBlock.Text" Value="{Binding Description11.Header}" />
                            </Style>
                        </TextBlock.Style>
                   </TextBlock>
               </Hyperlink.Inlines>
                </Hyperlink>
        </TextBlock>
    </Grid>
</Window>
很明显,我在XAML等中复制了代码。我想重构它,以便消除代码重复。为此,我考虑在ResourceDictionary中定义样式,然后将其绑定到MainWindowVM中的相应属性


但是我不知道怎么做,有什么想法吗?

你可以像这样轻松地在ResourceDictionary中移动样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <!-- Key is required to identify the Style -->
    <Style x:Key="Bind01" TargetType="TextBlock">
        <Setter Property="Text" Value="{Binding Description01.Header}" />
    </Style>

    <Style x:Key="Bind11" TargetType="TextBlock">
        <Setter Property="Text" Value="{Binding Description11.Header}" />        
    </Style>
</ResourceDictionary>
我们可以为按钮定义一个外观和功能相同的样式,但是变量绑定可以通过内容直接设置

按钮样式

    <Style x:Key="LinkStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <TextBlock>
                        <Hyperlink Command="{Binding Link}" CommandParameter="{Binding}">
                            <Run Text="{TemplateBinding Content}"/>
                        </Hyperlink>
                    </TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
屏幕虚线是网格线

编辑

要设置超链接的命令,我们使用按钮的Command属性设置绑定。因此,我们必须在样式中添加TemplateBinding。将硬编码命令替换为按钮命令的TemplateBinding。对Commandparameter执行相同的操作

             <Hyperlink Command="{TemplateBinding Command}"  
                   CommandParameter="{Templatebinding Commandparameter}"
                   Foreground="Blue" >

这应该如何回答我的问题?据我所知,您的目标是减少重复代码,对吗?也许我有点过于热情了,忘了提到这个解决方案可以简化两个文本块和超链接,而不是Hyperlink.Inlines的小样式部分。如果这不符合你的需要,我会更新我的答案。可以简化吗?你能详细说明并做出完整的答案吗?更新后,我希望这个解释更清楚,原始问题得到了回答。你的代码按钮样式不起作用-按钮在运行时根本不显示链接。您是否测试了自己的解决方案?
<TextBox Style="{DynamicResource Bind01}"/>
    <TextBlock>
        <Hyperlink Command="{Binding Link}"  
                   CommandParameter="{Binding}"
                   Foreground="Blue" >
           <Hyperlink.Inlines>
               <TextBlock>
                    <TextBlock.Style>
                        <Style>
                            <Setter Property="TextBlock.Text" Value="{Binding Description11.Header}" />
                        </Style>
                    </TextBlock.Style>
               </TextBlock>
           </Hyperlink.Inlines>
            </Hyperlink>
    </TextBlock>
    <Style x:Key="LinkStyle" TargetType="Button">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <TextBlock>
                        <Hyperlink Command="{Binding Link}" CommandParameter="{Binding}">
                            <Run Text="{TemplateBinding Content}"/>
                        </Hyperlink>
                    </TextBlock>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <TextBlock Grid.Row="0" Grid.Column="0" Text="ddk" />
    <Button Grid.Row="1" Grid.Column="1" 
            Content="{Binding Description01.Header}" 
            Style="{DynamicResource LinkStyle}">
    <TextBlock Grid.Row="1" Grid.Column="0" Text="dde" />
    <Button Grid.Row="1" Grid.Column="1" 
            Content="{Binding Description11.Header}" 
            Style="{DynamicResource LinkStyle}">
             <Hyperlink Command="{TemplateBinding Command}"  
                   CommandParameter="{Templatebinding Commandparameter}"
                   Foreground="Blue" >
    <TextBlock Grid.Row="0" Grid.Column="0" Text="ddk" />
<Button Grid.Row="1" Grid.Column="1" 
        Content="{Binding Description01.Header}"
        Command="{Binding YOURCOMMANDHERE}"
        CommandParameter="{YOURPARAMETER}" 
        Style="{DynamicResource LinkStyle}">
<TextBlock Grid.Row="1" Grid.Column="0" Text="dde" />
<Button Grid.Row="1" Grid.Column="1" 
        Content="{Binding Description11.Header}"
        Command="{Binding YOUROTHERCOMMANDHERE}"
        CommandParameter="{YOUROTHERPARAMETER}"
        Style="{DynamicResource LinkStyle}">